简体   繁体   English

如何与Laravel Eloquent回归多重关系?

[英]How to return multiple relationships with Laravel Eloquent?

I have a table called users . 我有一个名为users的表。 Each of these users has different things: 每个用户都有不同的东西:

  • 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: 我将这些值存储在users表中,如下所示:

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. 我正在考虑为users表中的每个事情做一个hasOne()关系,但后来我不确定如何立即获取/调用它们。

Anyone can help me tacke this issue? 有人可以帮我解决这个问题吗?

$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

Edit For Comment: 编辑评论:

Eloquent will assume the foreign key uses the Model name. Eloquent将假设外键使用Model名称。 So if your user table has the id column, it assumes there will be a column on device table called user_id . 因此,如果您的user表具有id列,则假定device表上将有一个名为user_id的列。 If you did this, then you are set and nothing should have to be done. 如果你这样做了,那么你就完成了,不需要做任何事情。

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. 相反,如果您想获取设备所属的用户,Eloquent将尝试将设备表上的user_id列与用户表上的id列进行匹配。 As before, if you used a different column to related them, declare the foreign key as the second argument. 和以前一样,如果您使用不同的列来关联它们,请将外键声明为第二个参数。

public function user()
{
    return $this->belongsTo('App\User', 'foreign_key_here',);
}

Laravel Doc One-To-One Relation Laravel Doc一对一关系

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. 查看文档以了解加载多个关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM