Martney Acha
Martney Acha

Reputation: 3002

Laravel Queries

I have 2 tables items and branch_item
I need to optimize and query this,

$test2 = DB::table('items')
            ->join('branch_item', 'items.id', '=', 'branch_item.item_id')
            ->select('items.minimum', 'branch_item.item_quantity')
            ->where('branch_item.branch_id',9)
            ->where('branch_item.item_quantity','<' ,'items.minimum')
            ->get();
return $test2;

What I need is to query the items that is below in minimum in quantity of certain branch.

I can do this using foreach but it loads so slow so I think I need to use join tables.

Upvotes: 1

Views: 64

Answers (1)

Martney Acha
Martney Acha

Reputation: 3002

$test2 = DB::table('branch_item')
            ->join('items', 'branch_item.item_id', '=', 'items.id')
            ->select('branch_item.id','items.id','items.minimum', 'branch_item.item_quantity')
            ->where('branch_item.branch_id',9)
            ->having('items.minimum', '>' ,'branch_item.item_quantity')
            ->get();

Our team debugs it already, what the problem is we use Where instead of Having, There is a big difference between having and where.

Upvotes: 1

Related Questions