php 如何使用 Laravel Eloquent 返回多个关系?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38544455/
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
How to return multiple relationships with Laravel Eloquent?
提问by Radical_Activity
I have a table called users
. Each of these users has different things:
我有一张桌子叫users
. 这些用户中的每一个都有不同的东西:
- country
- device
- computer
- category
- 国家
- 设备
- 计算机
- 类别
I have created a table for each of these above 'things'. Similar the following:
我为上述“事物”中的每一个都创建了一个表格。类似如下:
1 | United States
2 | United Kingdom
3 | Australia
4 | Canada
5 | Italy
etc...
等等...
I'm storing these values in the users table as follows:
我将这些值存储在用户表中,如下所示:
ID | Country | Device | Computer | Category |
---|---------|--------|----------|----------|
1 | 3 | 2 | 6 | 2 |
2 | 4 | 3 | 9 | 1 |
etc...
等等...
Now each of the above numbers are associated with the corresponding table's ID.
现在上面的每个数字都与相应的表的 ID 相关联。
What I want is do an Eloquent Query and search for all the users
and 'replacing' their corresponding values from their helper table.
我想要的是做一个 Eloquent Query 并users
从他们的帮助表中搜索所有和“替换”他们相应的值。
I was thinking about doing a hasOne()
Eloquent relationship for each of those things in the users
table, but then I'm not sure how to get/call them at once.
我正在考虑hasOne()
为users
表格中的每一项建立Eloquent 关系,但是我不确定如何立即获取/调用它们。
Anyone can help me tacke this issue?
任何人都可以帮我解决这个问题吗?
回答by Andrew Nolan
$model = Model::with('relatedModel', 'relatedModelTwo')->get();
So in your case, it can be something like this. If you only want one relations returned with the user
, remove the others.
所以在你的情况下,它可能是这样的。如果您只想用 返回一个关系user
,请删除其他关系。
$user = User::with('country', 'Device', 'Computer', 'Category')->get();
When you dd($user)
, you should see the related models in the relations
array attribute.
当你dd($user)
,你应该在relations
数组属性中看到相关的模型。
To access the relations' properties from there, it is simply
要从那里访问关系的属性,只需
$user->device->deviceAttribute
$user->device->deviceAttribute
Edit For Comment:
编辑评论:
Eloquent will assume the foreign key uses the Model name. So if your user
table has the id
column, it assumes there will be a column on device
table called user_id
. If you did this, then you are set and nothing should have to be done.
Eloquent 将假定外键使用模型名称。因此,如果您的user
表有该id
列,则假定device
表上将有一列名为user_id
. 如果你这样做了,那么你就准备好了,什么都不用做。
If you named your column to relate the models something else, you will need to pass that through as a second argument in the relation method.
如果您将列命名为与模型相关联,则需要将其作为关系方法中的第二个参数传递。
public function device()
{
return $this->hasOne('App\Device', 'foreign_key_here',);
}
Inversely, if you wanted to get the user the device belongs to, Eloquent will attempt to match the user_id
column on the device table to the id
column on the user table. As before, if you used a different column to related them, declare the foreign key as the second argument.
相反,如果您想获取设备所属的用户,Eloquent 会尝试user_id
将设备表中的id
列与用户表中的列进行匹配。和以前一样,如果您使用不同的列来关联它们,请将外键声明为第二个参数。
public function user()
{
return $this->belongsTo('App\User', 'foreign_key_here',);
}
回答by Daniel
You're looking for this: $books = App\Book::with('author', 'publisher')->get();
你正在寻找这个: $books = App\Book::with('author', 'publisher')->get();
Check out the documentation for eager loading multiple relationships.