Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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:

http://laravel.com/docs/5.1/eloquent-relationships

0

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.

Last updated 9 years ago.
0

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.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

sbrem00 sbrem00 Joined 2 Sep 2015

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.