[英]Laravel 5.5 eloquent cartesian
What I'm trying to accomplish, I believe, are cartesian results based upon related models. 我相信我要完成的工作是基于相关模型的笛卡尔结果。 I want to do this with eager loading.
我想通过热切的加载来做到这一点。
My eloquent query looks like this: 我雄辩的查询如下所示:
$results = PartBasePart::whereIn('id', array(1,2,3,4,5))
->with('materials')
->with('profiles')
->get();
And is yielding results of: 并产生以下结果:
+Collection
PartBasePart - Model
+Collection
Material - Model1
Material - Model2
Material - Model3
Material - Model4
Material - Model5
+Collection
Profile - Model1
What is desire is: 欲望是:
+Collection
PartBasePart - Model
+Collection
Material - Model1
+Collection
Profile - Model1
Material - Model2
+Collection
Profile - Model1
Material - Model3
+Collection
Profile - Model1
Material - Model4
+Collection
Profile - Model1
Material - Model5
+Collection
Profile - Model1
How is this possible? 这怎么可能? Can I get an instance of 'Profile - Model1' inside of each Material collection?
我可以在每个Material集合中获取“ Profile-Model1”的实例吗?
The models are defined as follows: 这些模型定义如下:
class PartBasePart extends Eloquent
{
public function materials()
{
return $this->belongsToMany(\App\Models\Material::class)
->withPivot('id');
}
public function profiles()
{
return $this->belongsToMany(\App\Models\Profile::class)
->withPivot('id');
}
}
class Material extends Eloquent
{
public function partBaseParts()
{
return $this->belongsToMany(\App\Models\PartBasePart::class)
->withPivot('id');
}
public function profiles()
{
return $this->belongsToMany(\App\Models\Profile::class)
->withPivot('id');
}
}
class Profile extends Eloquent
{
public function materials()
{
return $this->belongsToMany(\App\Models\Material::class)
->withPivot('id');
}
public function partBaseParts()
{
return $this->belongsToMany(\App\Models\PartBasePart::class)
->withPivot('id');
}
}
This should do it: 应该这样做:
$results = PartBasePart::whereIn('id', array(1,2,3,4,5))
->with('materials.profiles')
->get();
You want to tell Eloquent that you want the profiles related to the materials ('materials.profiles') rather than the profile related to the PartBasePart. 您想告诉Eloquent,您想要与材料相关的轮廓(“ materials.profiles”),而不是与PartBasePart相关的轮廓。 Your original query's
with
s were the equivalent of with(['materials', 'profiles'])
. 您的原始查询的
with
小号是相当于with(['materials', 'profiles'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.