2016-05-30 6 views
2

左のジョインにwhere句を追加すると、where句が右のテーブルで動作していますが、右のテーブルに一致する左のテーブルから結果が得られます。Laravel左結合where節を右のテーブルに追加する

$notifications = \DB::table('notifications') 
      ->select(\DB::raw("notifications.uuid ,images.local_path as title_image")) 
      ->leftJoin('images','images.owner_uuid', '=' ,'notifications.uuid') 
where('images.relation','=','notification_title') ; 

この問題は発生しませんが、このwhere句を左側の結合に追加するにはどうすればよいですか。

where('images.relation','=','notification_title') ; 
+0

「leftJoin」の前に 'where'節を追加する – hjpotter92

答えて

2

(左側のジョイン)右側のテーブルで動作するすべてのwhere句をJOINステートメント自体に追加する必要があります。このコードを使用する

$notifications = \DB::table('notifications') 
      ->select(\DB::raw("notifications.uuid , images.local_path as title_image")) 
      ->leftJoin('images',function ($join) { 
       $join->on('images.owner_uuid', '=' , 'notifications.uuid') ; 
       $join->where('images.relation','=','notification_title') ; 
      }); 
関連する問題