user3242861 user3242861 - 5 months ago 13
SQL Question

Laravel 5 - query builder AND

i have a mysql query and i want to pass them to laravel but i don't know how to use AND in laravel.

Mysql:

SELECT * FROM `items_sizes` left join `items` on `items_sizes`.`id_item` = `items`.`id` left join `budget__items` on `items_sizes`.`id` = `budget__items`.`id_itemSize` and `budget__items`.`id_budget` = 1 where `items`.`active` = 1 and `budget__items`.`id_budget` is null order by `items_sizes`.`updated_at` asc


Laravel:

$itemsSizes = ItemsSize::leftJoin('items', 'items_sizes.id_item', '=','items.id')
->leftJoin('budget__items','items_sizes.id', '=', 'budget__items.id_itemSize')
->*AND*('budget__items.id_budget', '=', $id)
->where('items.active', '=', 1)
->whereNull('budget__items.id_budget')
->orderBy('items_sizes.updated_at', 'asc')


Thank's

Answer

Please try the following:

$itemsSizes = ItemsSize::leftJoin('items', 'items_sizes.id_item', '=','items.id')
    ->leftJoin('budget__items', function($join) use($id) {
        $join->on('items_sizes.id', '=', 'budget__items.id_itemSize')
        ->where('budget__items.id_budget', '=', $id);
    })
    ->where('items.active', '=', 1)
    ->whereNull('budget__items.id_budget')
    ->orderBy('items_sizes.updated_at', 'asc');