[英]Laravel 5.2 Eloquent - Model through Many-To-Many Relationship
I have 3 models: Environments, Sites and Incidents. 我有3个模型:环境,站点和事件。 Each have their respective tables.
每个都有各自的表。
My model relationships are defined as follows: 我的模型关系定义如下:
Environments have many Sites ( environment_id
in sites table) 环境中有许多站点(站点表中的
environment_id
)
public function sites()
{
return $this->hasMany('App\Site');
}
Sites belong to an Environment ( environment_id
in sites table) and belong to many Incidents (incident_site relationship table with incident_id
and site_id
) 站点属于环境(站点表中的
environment_id
),并且属于许多突发事件(具有incident_id
和site_id
incident_site关系表)
public function environment()
{
return $this->belongsTo('App\Environment');
}
public function incidents()
{
return $this->belongsToMany('App\Incident');
}
Incidents belong to many Sites (incident_site relationship table with incident_id
and site_id
) 事件属于许多站点(具有
incident_id
和site_id
incident_site关系表)
public function sites()
{
return $this->belongsToMany('App\Site');
}
Problem: I am trying to retrieve a collection of all Site Incidents through the Environment model like this: 问题:我正在尝试通过环境模型来检索所有站点事件的集合,如下所示:
$environment->incidents()->count();
The only way I've been able to get it to work in a controller so far is like this: 到目前为止,我一直能够使其在控制器中工作的唯一方法是这样的:
$environment->load(['sites.incidents' => function ($q) use ( &$incidents ) {
$incidents = $q->orderBy('id','desc')->get();
}]);
But it's not ideal to work with in other areas of the App. 但是在应用程序的其他区域使用它并不是理想的。
Question: How do I go about making the above relationship work through a method in the Environment model? 问题:如何通过环境模型中的方法使上述关系起作用? Is there an easier way?
有更容易的方法吗?
There isn't any provision for using hasManyThrough()
in many-to-many relation. 没有在多对多关系中使用
hasManyThrough()
任何规定。 But you can achieve this by using either DB::raw()
or you can add following function to your BaseModel as given in this forum . 但是,您可以使用
DB::raw()
来实现此目的,也可以按照本论坛中的说明将以下函数添加到BaseModel中。
public function manyThroughMany($related, $through, $firstKey, $secondKey, $pivotKey)
{
$model = new $related;
$table = $model->getTable();
$throughModel = new $through;
$pivot = $throughModel->getTable();
return $model
->join($pivot, $pivot . '.' . $pivotKey, '=', $table . '.' . $secondKey)
->select($table . '.*')
->where($pivot . '.' . $firstKey, '=', $this->id);
}
Update: Use 更新:使用
first you would need to create a Model for incident_site
首先你需要创建一个模型
incident_site
class incident_site extends Model{
public $table = 'incident_site';
//your other code
}
In your Enviorment
model add the Incidents()
method: 在您的
Enviorment
模型添加的Incidents()
方法:
public function Incidents()
{
return $this->manyThroughMany('App\Incident', 'App\incident_site', 'site_id', 'id', 'incident_id');
}
Have modified the function according to your needs. 根据您的需要修改了功能。
Change your function to following: 将您的功能更改为以下内容:
public function manyThroughMany($related, $through ,$middle , $firstKey, $secondKey, $pivotKey)
{
$model = new $related;
$table = $model->getTable();
$throughModel = new $through;
$pivot = $throughModel->getTable();
$middleModel = new $middle;
$middleModelIds = $middleModel->where($this->getForeignKey(),$this->getKey())->get()->lists('id')->toArray();
//$middleModelIds = $this->with($middleModel)->where()->get()->lists('id')->toArray();
//$middleModelIds = $this->sites()->get()->lists('id')->toArray();
return $model
->join($pivot, $pivot . '.' . $pivotKey, '=', $table . '.' . $secondKey)
->select($table . '.*')
->whereIn($pivot . '.' . $firstKey,$middleModelIds);// '=', $this->id);
}
Use: 采用:
Extra argument of middle table needs to passed. 需要传递中间表的额外参数。
public function Incidents()
{
return $this->manyThroughMany('App\Incident', 'App\incident_site','App\Site','site_id', 'id', 'incident_id');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.