laravel LaravelbelongsToMany 只返回特定的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21646093/
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
Laravel belongsToMany to return specific columns only
提问by tiffanyhwang
So I have a many to many relationship between Users and Photos via the pivot table user_photo
. I use belongsToMany('Photo')
in my User model. However the trouble here is that I have a dozen columns in my Photo table most I don't need (especially during a json response). So an example would be:
所以我通过数据透视表在用户和照片之间建立了多对多的关系user_photo
。我belongsToMany('Photo')
在我的用户模型中使用。然而,这里的问题是我的照片表中有十多列我最不需要(尤其是在 json 响应期间)。所以一个例子是:
//Grab user #987's photos:
User::with('photos')->find(987);
//json output:
{
id: 987,
name: "John Appleseed",
photos: {
id: 5435433,
date: ...,
name: 'feelsgoodman.jpg',
....// other columns that I don't need
}
}
Is it possible to modify this method such that Photos
model will only return the accepted columns (say specified by an array ['name', 'date']
)?
是否可以修改此方法,使Photos
模型仅返回接受的列(例如由数组指定['name', 'date']
)?
User.php
用户名.php
public function photos()
{
return $this->belongsToMany('Photo');
}
Note: I only want to select specific columns when doing a User->belongsToMany->Photo
only. When doing something like Photo::all()
, yes I would want all the columns as normal.
注意:我只想在做 a User->belongsToMany->Photo
only时选择特定的列。在做类似的事情时Photo::all()
,是的,我希望所有的列都正常。
EDIT: I've tried Get specific columns using "with()" function in Laravel Eloquentbut the columns are still being selected. Also https://github.com/laravel/laravel/issues/2306
编辑:我已经尝试在 Laravel Eloquent 中使用“with()”函数获取特定列,但这些列仍在被选中。还有https://github.com/laravel/laravel/issues/2306
回答by Vivek Pandey
You can use belongsToMany with select operation using laravel relationship.
您可以使用 laravel 关系将belongsToMany 与选择操作一起使用。
public function photos()
{
return $this->belongsToMany('Photo')->select(array('name', 'date'));
}
回答by jah
Im assuming you have a column named user_id. Then you should be able to do the following:
我假设您有一个名为 user_id 的列。那么你应该能够做到以下几点:
public function photos()
{
return $this->belongsToMany('Photo')->select(['id', 'user_id', 'date', 'name']);
}
You have to select, the foreign key, else it will have no way of joining it.
您必须选择外键,否则它将无法加入。
回答by Aken Roberts
Specifying the exact columns you want for the Photos
relationship will likely end up biting you in the butt in the future, should your application's needs ever change. A better solution would be to only specify the data you want to return in that particular instance, i.e. the specific JSON response you're delivering.
Photos
如果您的应用程序的需求发生变化,那么为关系指定您想要的确切列可能会在将来让您大吃一惊。更好的解决方案是仅指定您要在该特定实例中返回的数据,即您提供的特定 JSON 响应。
Option 1: extend/overwrite the toArray()
Eloquent function (which is called by toJson()
) and change the information returned by it. This will affect everycall to these methods, though, so it may end up giving you the same problems as doing select()
in the original query.
选项 1:扩展/覆盖toArray()
Eloquent 函数(由 调用toJson()
)并更改其返回的信息。但是,这将影响对这些方法的每次调用,因此最终可能会给您带来与select()
原始查询中相同的问题。
Option 2: Create a specific method for your JSON response and call it instead of the general toJson()
. That method could then do any data building / array modifications necessary to achieve the specific output you need.
选项 2:为您的 JSON 响应创建一个特定的方法并调用它而不是一般的toJson()
. 然后,该方法可以进行任何必要的数据构建/数组修改,以实现您需要的特定输出。
Option 3: If you're working with an API or ajax calls in general that need a specific format, consider using a library such as League/Fractal, which is built for just such an occasion. (Phil is also working on a book on building APIs, and it doesn't suck.)
选项 3:如果您正在使用 API 或一般需要特定格式的 ajax 调用,请考虑使用诸如League/Fractal 之类的库,该库专为此类情况而构建。(Phil 也在写一本关于构建 API的书,它并不烂。)