Support the ongoing development of →
Database Eloquent
Last updated 1 year ago.

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?


EDIT: I just realised this does not work and I get an error that $order is undefined. Any help would be much appreciated.

Last updated 8 years ago.

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){

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.

Last updated 8 years ago.

Sign in to participate in this thread!


Your banner here too?

sbrem00 sbrem00 Joined 2 Sep 2015


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.