i'm using whereRelation but i do not know how to take the where value condition from the base model
I have tried this code:
Item::with('unit','stock')->whereRelation('stock', 'stock', '<', 'items.min_stock');
and query result in debugger :
select * from `items` where exists (select * from `stocks` where `items`.`id` = `stocks`.`id_item` and `stock` < 'items.min_stock')
The query result i wanted :
select * from `items` where exists (select * from `stocks` where `items`.`id` = `stocks`.`id_item` and `stock` < `items`.`min_stock`)
'items.min_stock' it become like a string, How can i solve this ?
I found solution for my code
i change my code like this and it's works
Item::with('unit','stock')->whereRelation('stock', 'stock', '<', DB::raw('items.min_stock'));
thanks to @Vildan Bina for the answer
Try using whereRaw
Item::with('unit','stock')->whereRelation('stock', function (Builder $query) {
$query->whereRaw('stock < items.min_stock');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With