Laravel $appends 未显示在结果中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35609824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:17:54  来源:igfitidea点击:

Laravel $appends not showing in results

phplaravel

提问by Get Off My Lawn

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

回答by aynber

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.

将属性添加到 appends 列表后,它将包含在模型的数组和 JSON 表单中。appends 数组中的属性也将尊重模型上配置的可见和隐藏设置。

If you use toArray() or toJson():

如果使用 toArray() 或 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.

它将是可见的。

回答by Kavin Mehta

This is for future in case someone else comes to this page with a similar (not exact) issue via google: I had similar issues, turns out I was not using Eloquent correctly, I had appends in my model - EventyTypes but I was getting data using $eventTypes = DB::table('event_types')->get(); which was incorrect. I then modified it to $eventTypes = EventTypes::get();and it worked. This makes sense since the appends is in Model you should get data from model and not using DB.

这是为了将来万一其他人通过谷歌遇到类似(不完全)问题来到这个页面:我有类似的问题,结果我没有正确使用 Eloquent,我的模型中有附加 - EventyTypes 但我正在获取数据使用$eventTypes = DB::table('event_types')->get(); 这是不正确的。然后我将其修改为 $eventTypes = EventTypes::get();并且它起作用了。这是有道理的,因为追加在模型中,您应该从模型中获取数据而不是使用 DB。

回答by Giles Correia Morton

Another slightly different but similar issue I ran into around this was using the with()method.

我遇到的另一个稍微不同但类似的问题是使用该with()方法。

I had Event::with(['event_types:id,name,url'])where url was defined by getUrlAttribute()in the EventTypemodel. But this wasn't working because my syntax event_types:id,name,urlattempts to load specific columns which isn't compatible.

我在模型中Event::with(['event_types:id,name,url'])定义了 url 。但这不起作用,因为我的语法尝试加载不兼容的特定列。getUrlAttribute()EventTypeevent_types:id,name,url

To fix the issue I removed this syntax and just did Event::with(['event_types'])which loaded everything as expected.

为了解决这个问题,我删除了这个语法,只是Event::with(['event_types'])按预期加载了所有内容。