A JOIN With Additional Conditions Using Query Builder or Eloquent

后端 未结 6 687
抹茶落季
抹茶落季 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.
    0 讨论(0)
  • 2020-12-04 09:16

    I am using laravel5.2 and we can add joins with different options, you can modify as per your requirement.

    Option 1:    
        DB::table('users')
                ->join('contacts', function ($join) {
                    $join->on('users.id', '=', 'contacts.user_id')->orOn(...);//you add more joins here
                })// and you add more joins here
            ->get();
    
    Option 2:
        $users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')// you may add more joins
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();
    
    option 3:
        $users = DB::table('users')
            ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
            ->leftJoin('...', '...', '...', '...')// you may add more joins
            ->get();
    
    0 讨论(0)
  • 2020-12-04 09:28

    The sql query sample like this

    LEFT JOIN bookings  
        ON rooms.id = bookings.room_type_id
        AND (bookings.arrival = ?
            OR bookings.departure = ?)
    

    Laravel join with multiple conditions

    ->leftJoin('bookings', function($join) use ($param1, $param2) {
        $join->on('rooms.id', '=', 'bookings.room_type_id');
        $join->on(function($query) use ($param1, $param2) {
            $query->on('bookings.arrival', '=', $param1);
            $query->orOn('departure', '=',$param2);
        });
    })
    
    0 讨论(0)
  • 2020-12-04 09:29

    You can replicate those brackets in the left join:

    LEFT JOIN bookings  
                   ON rooms.id = bookings.room_type_id
                  AND (  bookings.arrival between ? and ?
                      OR bookings.departure between ? and ? )
    

    is

    ->leftJoin('bookings', function($join){
        $join->on('rooms.id', '=', 'bookings.room_type_id');
        $join->on(DB::raw('(  bookings.arrival between ? and ? OR bookings.departure between ? and ? )'), DB::raw(''), DB::raw(''));
    })
    

    You'll then have to set the bindings later using "setBindings" as described in this SO post: How to bind parameters to a raw DB query in Laravel that's used on a model?

    It's not pretty but it works.

    0 讨论(0)
  • 2020-12-04 09:29

    There's a difference between the raw queries and standard selects (between the DB::raw and DB::select methods).

    You can do what you want using a DB::select and simply dropping in the ? placeholder much like you do with prepared statements (it's actually what it's doing).

    A small example:

    $results = DB::select('SELECT * FROM user WHERE username=?', ['jason']);
    

    The second parameter is an array of values that will be used to replace the placeholders in the query from left to right.

    0 讨论(0)
  • 2020-12-04 09:35

    If you have some params, you can do this.

        $results = DB::table('rooms')
        ->distinct()
        ->leftJoin('bookings', function($join) use ($param1, $param2)
        {
            $join->on('rooms.id', '=', 'bookings.room_type_id');
            $join->on('arrival','=',DB::raw("'".$param1."'"));
            $join->on('arrival','=',DB::raw("'".$param2."'"));
    
        })
        ->where('bookings.room_type_id', '=', NULL)
        ->get();
    

    and then return your query

    return $results;

    0 讨论(0)
提交回复
热议问题