A JOIN With Additional Conditions Using Query Builder or Eloquent

后端 未结 6 698
抹茶落季
抹茶落季 2020-12-04 09:08

I\'m trying to add a condition using a JOIN query with Laravel Query Builder.



        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 09:15

    $results = DB::table('rooms')
                         ->distinct()
                         ->leftJoin('bookings', function($join)
                             {
                                 $join->on('rooms.id', '=', 'bookings.room_type_id');
                                 $join->on('arrival','>=',DB::raw("'2012-05-01'"));
                                 $join->on('arrival','<=',DB::raw("'2012-05-10'"));
                                 $join->on('departure','>=',DB::raw("'2012-05-01'"));
                                 $join->on('departure','<=',DB::raw("'2012-05-10'"));
                             })
                         ->where('bookings.room_type_id', '=', NULL)
                         ->get();
    

    Not quite sure if the between clause can be added to the join in laravel.

    Notes:

    • DB::raw() instructs Laravel not to put back quotes.
    • By passing a closure to join methods you can add more join conditions to it, on() will add AND condition and orOn() will add OR condition.

提交回复
热议问题