2017-04-15 2 views
0

isrecurring = 1をピボットテーブルに格納しますが、既存の解決策が見つかりません。誰も経験を持っており、多対多のピボットテーブルで'isrecurring' = 1のすべてのレコードを取得する方法はありますか? (wherePivotなし)Laravel:ポリモーフィック多対多でwherePivotを使用する方法

Example. 
$enroll->products->where('isrecurring', 1); 

but in enrollable pivot table 
-> get all records that 'isrecurring' = 1 

マイモデル

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

私は同じ問題を持っていたし、次を使用して、それを解決することができます:

//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); 

ピボットテーブルのカスタムSQLを追加するためにfunction($query){ }を使用

ここでLaravel docsの詳細を読むことができます: https://laravel.com/docs/5.5/eloquent-relationships#querying-relationship-existenceクエリの関係存在の部分

関連する問題