Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel $appends not showing in results

Tags:

php

laravel

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);
like image 617
Get Off My Lawn Avatar asked Dec 01 '22 16:12

Get Off My Lawn


1 Answers

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.

like image 80
aynber Avatar answered Dec 03 '22 22:12

aynber