【发布时间】:2016-05-25 00:50:40
【问题描述】:
我配置了 5 个模型。客户、环境、对象、服务角色和服务。我已经在每个模型中建立了适当的雄辩关系。
客户有许多环境。
//Customer Model
public function environments()
{
return $this->hasMany('App\Environment');
}
环境属于一个客户。
环境属于许多对象。
//Environment Model
public function customer()
{
return $this->belongsTo('App\Customer');
}
public function objects()
{
return $this->belongsToMany('App\Object');
}
对象属于许多环境。
对象属于许多 ServiceRoles。
//Object Model
public function environments()
{
return $this->belongsToMany('App\Environment');
}
public function serviceRoles()
{
return $this->belongsToMany('App\ServiceRole');
}
ServiceRoles 属于许多对象。
ServiceRoles 属于一个 Service。
//ServiceRole Model
public function objects()
{
return $this->belongsToMany('App\Object');
}
public function service()
{
return $this->belongsTo('App\Service');
}
服务属于许多服务角色。
public function serviceRoles()
{
return $this->hasMany('App\ServiceRole');
}
--SQL--
客户:id、名称
对象:id、名称
环境: id、name、customer_id
environment_object:id、environment_id、object_id
service_roles:id、name、service_id
object_service_role : id, object_id, service_role_id
服务: id, name
1) 检索与客户关联的所有对象(跨所有相关环境)的最简单方法是什么?
想做类似的事情:$customer->objects
2) 我怎样才能检索与客户关联的对象的所有服务,因为每个对象都有一个映射到服务的 ServiceRole。
想做类似的事情:$customer->services
【问题讨论】:
-
抱歉,我添加了模型并稍微修改了我的第二个问题以反映我错过的另一个模型。谢谢
标签: mysql laravel eloquent many-to-many one-to-many