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

Paste the definitions of those relations.

Last updated 2 years ago.
0

What I am trying to achieve is define multi level relationship constraint in my model.

http://laravel.io/bin/448X

These are the definitions of my models.

As can be seen in the paste, I have one to many relation of Biblio with BiblioItem using items() and BiblioItem is having one to one relation with BiblioItemCondition

Now I want to apply relation constraint using whereHas() on BiblioItemCondition in Biblio model.

Last updated 2 years ago.
0

jarektkaczyk said:

Check this: http://laravel.io/bin/wEaQ#112-115,117-119

There is a one to many relation between Biblio and BiblioItem as defined by items() and one to one relation between BiblioItem and BiblioItemCondition as defined by condition() in BiblioItem

It is an hierarchical relation here.

Last updated 2 years ago.
0

I have been able to achieve this by modifying my whereHas() statement like this

Biblio::whereHas('items', function($q) 
                use ($filters) {
                $q->whereHas('condition', function($q) 
                    use($filters) {
                    $q->where('library_biblio_item_conditions.name', $filters['condition']);
                });
            });

Actually I have nested whereHas() for BiblioItem and BiblioItemCondition

This worked, but I would like to know, if this is the correct way to do it.

Last updated 2 years ago.
0

You defined your relations like this:

Biblio hasMany Items, Condition hasMany Items.

So either you say something different to what you need, or these relations were wrong. Not mentioning that hasMany() does not take table name as a 2nd param.

and for whereHas - it works with dot nested relations, so you don't need to use the it the way you did in your recent post.

Last updated 2 years ago.
0

Yes, >jarektkaczyk said:

You defined your relations like this:

Biblio hasMany Items, Condition hasMany Items.

So either you say something different to what you need, or these relations were wrong. Not mentioning that hasMany() does not take table name as a 2nd param.

and for whereHas - it works with dot nested relations, so you don't need to use the it the way you did in your recent post.

Yes Biblio hasMany Items and Condition hasMany Items, sorry for the typo in my previous post and thanks for the guidance and insight.

But the dot nested relations is not working in my case for whereHas().

As per the paste, i should be able to access the relation with items.condition. Right?? But I am getting error while trying to do so.

Call to undefined method Illuminate\Database\Query\Builder::items.condition() 

Please help.

Last updated 2 years ago.
0

Hmmmm I must admit I was wrong here! whereHas() doesn't work with dot nested relations, that's all. Your code is OK. I was pretty sure about that, while it was just with() that worked this way, sorry for that.

However I will take a deeper look at the code of whereHas and check how to refine that to work with nested relations. If it happens to be simple I'll post it here, otherwise I'll send a pull request first so Taylor can have a look and check it.

Last updated 2 years ago.
0

I haven't read the whole thing, but this is how I use whereHas with nested models:

		$search = Input::get('search');

		$orders = Order::with(array(
							'OrderProduct',
							'OrderProduct.Product',
							'OrderProduct.Product.ProductCategory'
							)
						);

		$orders->whereHas('OrderProduct', function($q) use($search){ 
			$q->whereHas('Product', function($q) use($search) {
				$q->whereHas('ProductCategory', function($q) use($search) {
					$q->where('name','like','%'.$search.'%');
				});
			});
		});

If I wanted to search, for like description in the ProductCategory, I would need to do another code block like that but with a orWhereHas on the 1st level, since orWhere() wont work after the normal $q->where()


					$q->where('name','like','%'.$search.'%');
					$q->orWhere('description','like','%'.$search.'%');

The above returns inaccurate results.

Last updated 2 years ago.
0

Thank you @jarektkaczyk for your valuable insights. Would love to know more about the nested thing, if that is done.

Thank you @makzumi for a working example. This is how I did it in my code, but was looking for a cleaner way.

Last updated 2 years ago.
0

You can check PR https://github.com/laravel/framework/pull/4237 and use it if it suites you.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.