Have you read through the docs on relationships? Once you have set up a relationship in your model, and you grasped the concept, you should be flying.
Have a bash at these and if anything doesn't make sense, give me a shout:
Thanks for your response. I have given the link a read and think I get the idea. I have setup the relationships in the models so as i understand i can do something like:
$supplierPO = SupplierPO::where(function($q){
$q->order->cartItems->where('supplier_id', '=', '123456')->count();
}, '=', 0)->get()
where order is a relationship defined in the SupplierPO modal and cartItems is a relationship defined in the Orders model.
The aim is to return all the rows in SupplierPO that don't have any cart items with the supplier_id 123456
I need to do some testing to see if this works but can you confirm if I am going along the right lines in terms of traversing through the different models?
Thanks.
EDIT: I just realised this does not work and I get an error that $order is undefined. Any help would be much appreciated.
Just to update, the way I have managed to do this is as follows:
$supplierPO = SupplierPO::with('order')->get();
foreach($supplierOrderItems as $key => $item){
$countOfItems = LocalCartItems::where('cart_id', '=', $item->order->cart_id)->where('selected_supplier_id', '=', '123456')->count();
if($countOfItems > 0){
$supplierOrderItems->forget($key);
}
}
not sure if there may be a better way by using
SupplierPO::with(['order', 'order.cartItems'])->get();
If anyone knows a better way of keeping this in one query please let me know.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community