Laravel | Return 来自多个模型的多个表
Laravel | Return multiple tables from multiple models
我正在尝试使用 Laravel/Lumen Eloquent 链接几个模型。
我有一个基本的数据库设置,其中包含以下 tables:
- 客户
- customer_websites
- customer_websites_status
我正在尝试获取我拥有的所有客户详细信息,然后获取我也拥有的关联网站,但我想从第三个 table 获取网站状态。
这是我目前的代码。
控制器:
$customer->with('website')->whereCustomerHash(123456)->first();
客户:
public function website()
{
return $this->hasOne('App\CustomerWebsite');
}
客户网站:
public function status()
{
return $this->hasOne('App\CustomerWebsiteStatus');
}
客户网站状态:
public function website()
{
return $this->belongsTo('App\CustomerWebsite');
}
我要return的格式是
{
first_name: 'Joe',
last_name: 'Blogs',
website: {
url: 'http://google.com',
status: 'Status Here.'
}
}
如果你运行:
$myCustomer = $customer->with([
'website','website.status'
])->whereCustomerHash(123456)->first();
然后你可以获得$myCustomer->website->status->description
或者任何你拥有的状态table。
我正在尝试使用 Laravel/Lumen Eloquent 链接几个模型。
我有一个基本的数据库设置,其中包含以下 tables:
- 客户
- customer_websites
- customer_websites_status
我正在尝试获取我拥有的所有客户详细信息,然后获取我也拥有的关联网站,但我想从第三个 table 获取网站状态。
这是我目前的代码。
控制器:
$customer->with('website')->whereCustomerHash(123456)->first();
客户:
public function website()
{
return $this->hasOne('App\CustomerWebsite');
}
客户网站:
public function status()
{
return $this->hasOne('App\CustomerWebsiteStatus');
}
客户网站状态:
public function website()
{
return $this->belongsTo('App\CustomerWebsite');
}
我要return的格式是
{
first_name: 'Joe',
last_name: 'Blogs',
website: {
url: 'http://google.com',
status: 'Status Here.'
}
}
如果你运行:
$myCustomer = $customer->with([
'website','website.status'
])->whereCustomerHash(123456)->first();
然后你可以获得$myCustomer->website->status->description
或者任何你拥有的状态table。