ORMまだ使っていなくてクエリビルダでほぼSQLを書いてます。
joinしてるとき条件によってjoinの中でwhere句を動的に追加したい場合の記述
$articles = DB::table('user')
->join('article', function ($join) use ($request) {
$join->on('user.id', '=', 'article.hoge_id');
// リクエストでいいねがあるものだけとりたいといわれたとき
if ($request->has('good')) {
$join->where('article.good_flag', '=', 1);
}
})->get();
ReadDouble
Laravel 5.6 データベース:クエリビルダ
上級のJOIN文
https://readouble.com/laravel/5.6/ja/queries.html#joins