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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:36:58  来源:igfitidea点击:

How to return multiple relationships with Laravel Eloquent?

phplaravellaravel-5.2

提问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 usersand '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 userstable, 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 relationsarray 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 usertable has the idcolumn, it assumes there will be a column on devicetable 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_idcolumn on the device table to the idcolumn 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',);
}

Laravel Doc One-To-One Relation

Laravel Doc 一对一关系

回答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.

查看文档以了解预先加载多个关系。