Alan Cordova
Alan Cordova

Reputation: 171

Multiple Joins Laravel

i need to make two joins but with many conditionals where, i have this

    $matchThese = [ 'suspended' => 0, 'status' => 1, 'approved' => 1 ];

    $matchOther = [ 'status' => 1, 'approved' => 0 ];

    $deals = ListsDeals::where( $matchThese )->where('stock', '>', 0)->orwhere( $matchOther )->whereDate('end_date', '>', date('Y-m-d'))->limit( 4 )->offset( 0 )->orderBy( 'start_date' )->get();

    $deals_lists = DB::table('deals')
                ->join( 'list_has_deals', 'deals.id', '=', 'list_has_deals.deal_id'  )
                ->join( 'lists', 'list_has_deals.list_id', '=', 'lists.id' )
                ->paginate( 10 );

I need one unique query with that two vars, select all deals with the 'WHERES' in the first var and later make the joins, regards.

Upvotes: 0

Views: 221

Answers (1)

C Taque
C Taque

Reputation: 1097

I think that could do the trick :

$query = ListsDeals::where( $matchThese )->where('stock', '>', 0)->orwhere( $matchOther )->whereDate('end_date', '>', date('Y-m-d'))->limit( 4 )->offset( 0 )->orderBy( 'start_date' );

$results = $query->join( 'list_has_deals', 'deals.id', '=', 'list_has_deals.deal_id'  )
            ->join( 'lists', 'list_has_deals.list_id', '=', 'lists.id' )
            ->paginate( 10 );

Upvotes: 2

Related Questions