简体   繁体   English

如何从 Eloquent 模型中获取所有数据及其关系?

[英]How to get all data from a Eloquent Model with it's relations?

My question is how to get the data from an eloquent model with its relation?我的问题是如何从具有其关系的雄辩模型中获取数据?

Imagine 3 tables:想象一下 3 个表:

users :用户

| id | name | email           |
|----|------|-----------------|
| 1  | name | email@email.com |

companies :公司

| id | name | user_id | address_id |
|----|------|---------|------------|
| 1  | name | 1       | 1          |

addresses :地址

| id | zip    | state |
|----|--------|-------|
| 1  | 101010 | LA    |

And the relations:和关系:

Company.php公司.php

public function user()
{
    return $this->hasOne(User::class);
}

public function address()
{
    return $this->hasOne(Address::class);
}

User.php用户名

public function company()
{
    return $this->belongsTo(Company::class);
}

Address.php地址.php

public function company()
{
    return $this->belongsTo(Company::class);
}

In this case how I can get let's say all the companies with their related users and addresses?在这种情况下,我如何才能让我们说所有公司及其相关用户和地址?

on Company::whereHas('users')->get(); on Company::whereHas('users')->get(); getting:得到:

Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.company_id' in 'where clause' (SQL: select * from `users` where `users`.`company_id` in (1, 2, 3, 4, 5))'

Any thoughts?有什么想法吗?

Thank you谢谢

You need to swap the hasOne with BelongsTo and vice versa;您需要将 hasOne 与 BelongsTo 交换,反之亦然;

Company.php公司.php

public function users()
{
    return $this->belongsTo(User::class, 'user_id');
}

public function address()
{
    return $this->belongsTo(Address::class);
}

User.php用户名

public function company()
{
    return $this->hasOne(Company::class);
}

Address地址

public function company()
{
    return $this->hasOne(Company::class);
}

and then in Controller然后在控制器中

Company::whereHas('users')
    ->whereHas('address')
    ->with('users')->with('address')
    ->get();

The issue was that in the company model, the user and the address methods were referencing with the hasOne() method to other models.问题是在公司模型中,用户和地址方法使用hasOne()方法引用其他模型。 It was looking for a secondary key to be in the users' table, not in the companies one.它正在寻找用户表中的辅助键,而不是公司表中的辅助键。

Simply swapping up the hasOne() with belongsTo fixed out my issue.简单地将hasOne()belongsTo交换belongsTo解决我的问题。

So now my Company.php model looks like:所以现在我的Company.php模型看起来像:

public function user()
{
    return $this->belongsTo(User::class);
}

public function address()
{
    return $this->belongsTo(Address::class);
}

User.php用户名

public function company()
{
    return $this->hasOne(Company::class);
}

Address.php地址.php

public function company()
{
    return $this->hasOne(Company::class);
}

Now we can get all the list of companies with their user and address by Company::with('user')->with('address')->get();现在我们可以通过Company::with('user')->with('address')->get();获取所有公司及其用户和地址的列表Company::with('user')->with('address')->get();

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

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