I am trying to append a value to my model, but it isn't getting added. Am I doing something incorrectly?
Here is what I am defining:
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);
}
When I do a dump, It doesn't show up in the results here is the call to the model:
$items = GamesModel::select('title', 'games.user_id', 'games.game_id', 'games.created_at', 'icon_large', 'icon_medium', 'icon_small', 'short_description')
->GetDeveloperGames($userid)
->get();
dd($items);
From the docs
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.
If you use toArray() or toJson():
$items = GamesModel::select('title', 'games.user_id', 'games.game_id', 'games.created_at', 'icon_large', 'icon_medium', 'icon_small', 'short_description')
->GetDeveloperGames($userid)
->get()
->toArray();
It will be visible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With