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 ?
CodePudding user response:
Try using whereRaw
Item::with('unit','stock')->whereRelation('stock', function (Builder $query) {
$query->whereRaw('stock < items.min_stock')
});
CodePudding user response:
Otherize you can use whereHas
method, example below:
Item::with('unit', 'stock')->whereHas('stock', function ($query) {
$query->where('stock', '<', DB::raw('items.min_stock'))
});
CodePudding user response:
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