[PHP][Laravel] データベースクエリビルダ(Model)でleft joinの絞り込みon条件にパラメータを渡して複数指定する
バージョン
Laravel 5.8
概要
LEFT JOINのON条件を複数指定する。
条件にはパラメータを渡したい。
コード
◾️Model
$users = DB::table('users') ->leftJoin('posts as user_posts', function ($join) use ($type) { $join->on('users.id', '=', 'user_posts.user_id') ->where('user_posts.type', $type); }) ->get();
パラメータはuse ($variable)
で渡す。
下記のようなコードでも可能。
$users = DB::table('users') ->leftJoin('posts as user_posts', function ($join) use ($type) { $join->on('users.id', '=', 'user_posts.user_id'); $join->on('user_posts.type', '=', $type); }) ->get();
- 作者:津耶乃, 掌田
- 発売日: 2019/12/27
- メディア: 単行本