简体   繁体   中英

Laravel 5 query builder, complicated join (condition AND (condition OR condition))

I need to reproduce the following query in Laravel's query builder:

select * from table LEFT JOIN table_b ON `condition` AND (`condition` OR `condition`)

I know you can do complicated where statements like this:

Model::where(function ($query) {
  return $query->where("field","value")->orWhere("field","value");
})->where("field","value")->get();

And I know you can do semi-complicated on statements like this:

->leftJoin('table', function ($join) {
  $join->on("field","=","field_b")
    ->on("field_a","=","field_c");
})

But I need to have condition AND (condition OR condition) INSIDE an on statement. Is this possible without doing DB::raw() or equivalent?

Use this:

->leftJoin('table', function ($join) {
    $join->on("field","=","field_b")
        ->on(function($join) {
            $join->on("field_a","=","field_c")
                ->orOn("field_d","=","field_e");
        });
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM