2017-02-24 13 views
2

私はコースとサブスクリプションの種類があります。 サブスクライブされたユーザーにサブスクリプションタイプがあります。ユーザーサブスクリプションタイプが一致するすべてのコースが必要です。Laravel 5の関係が動作しません

$courses = Course::has('maintask')->with(['subscriptionType' => function ($q) use ($user) 
     { 
      return $q->where('subscription_type_id',$user->subscription[0]->subscription_type_id)->where('status','1'); 
     }])->get(); 

任意のヒント:(すべてのコースだけでなく、右のものを返します)

私の試み?

ありがとうございます!

答えて

3

使用whereHas()

$courses = Course::has('maintask') 
    ->whereHas('subscriptionType', function ($q) use ($user) { 
     $q->where('id', $user->subscription[0]->subscription_type_id) 
      ->where('status', 1); 
    })->get(); 
+1

感謝!!それは働く – user3844579

関連する問題