我想在我的数据透视表中查询 isrecurring = 1 的位置,但未找到现有解决方案。任何人都有任何经验以及如何在多对多多态数据透视表中获取'isrecurring'= 1 的所有记录?
Example.
$enroll->products->where('isrecurring', 1);
but in enrollable pivot table
-> get all records that 'isrecurring' = 1
我的模特(没有wherePivot)
Enroll.php
------
public function products(){
return $this->morphedByMany('App\Models\Product', 'enrollable')->withPivot('isrecurring');
}
Product.php
----
public function enrolls(){
return $this->morphToMany('App\Models\Enroll', 'enrollable')->withPivot('isrecurring');
}
我的数据库
enrolls
-----
id
products
----
id
enrollables
----
enroll_id
enrollable_id
enrollable_type
isrecurring (boolean)
我希望使用wherePivot,但似乎无法正常工作且无法查询。
Product.php
----
public function enrolls(){
return $this->morphToMany('App\Models\Enroll', 'enrollable')->withPivot('isrecurring')->wherePivot('isrecurring', '=', 1);
}
答案 0 :(得分:0)
我遇到了同样的问题,可以使用以下方法解决问题:
//Model: User.php
public function certificates()
{
return $this->morphedByMany('App\Certificate', 'appliable')
->withTimestamps();
}
//Controller: EnrollmentController.php
$usersWithCertificates = User::whereHas('certificates', function($query){
$query->where('status', '0');
})->with('certificates')->latest()->paginate(10);
使用function($query){ }
添加数据透视表的自定义SQL
您可以在此处阅读Laravel文档中的更多内容: 查询关系存在部分中的https://laravel.com/docs/5.5/eloquent-relationships#querying-relationship-existence