我试图将一个值附加到我的模型,但它没有被添加.我做错了什么吗?
这是我定义的:
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();
它将是可见的.