php – Laravel $追加不显示在结果中

前端之家收集整理的这篇文章主要介绍了php – Laravel $追加不显示在结果中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图将一个值附加到我的模型,但它没有被添加.我做错了什么吗?

这是我定义的:

protected $appends = ['hash'];

public function getHashAttribute(){
    return 'test';
}

public function scopeGetDeveloperGames($query,$userid){
    return $query
        ->join('game_info','games.game_id','=','game_info.game_id')
        ->where('games.user_id',$userid)
        ->orderBy('games.created_at','desc')->limit(9);
}

当我进行转储时,结果中没有显示这里是对模型的调用

$items = GamesModel::select('title','games.user_id','games.created_at','icon_large','icon_medium','icon_small','short_description')
->GetDeveloperGames($userid)
->get();
dd($items);

解决方法

来自文档

Once the attribute has been added to the appends list,it will be included in both the model’s array and JSON forms. Attributes in the appends array will also respect the visible and hidden settings configured on the model.

如果使用toArray()或toJson():

$items = GamesModel::select('title','short_description')
->GetDeveloperGames($userid)
->get()
->toArray();

它将是可见的.

猜你在找的Laravel相关文章