加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 19:35:01 所属栏目:大数据 来源:网络整理
导读:我试图将一个值附加到我的模型,但它没有被添加.我做错了什么吗? 这是我定义的: protected $appends = ['hash'];public function getHashAttribute(){ return 'test';}public function scopeGetDeveloperGames($query,$userid){ return $query -join('game_
我试图将一个值附加到我的模型,但它没有被添加.我做错了什么吗?

这是我定义的:

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();

它将是可见的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读