Support the ongoing development of →
posted 10 years ago
Last updated 2 years ago.

I wonder why you would want that ?

Anyway, you can't do any better with native Eloquent methods, but with a little bit of magic Laravel let you do that.

Imagine Category that has many posts (your b1) and has many _posts (your b2). You would like to load all posts for given Category in one query and set the relations accordingly.

Also you would like to be able to load the posts for a Collection of Categories.

So here's how to make it work:

There are 2 methods loadPosts() on the model and custom collection for this task.

Check this out and try to make the nested relation work alike.

Last updated 2 years ago.

@jarektkaczyk, Thank you that's what I did manually, but I better use $relation->match like you did, I think this optimization would be a good feature to have out of the box.

A synatx like this:

$a = A::all();
$a->load('b1:B', 'b2:B', 'b1.c:C', 'b2.c:C');

Or maybe just let Eloquent deduce Models from relation.

Last updated 2 years ago.

what about something like this ?

$a = A::with('b1.c', 'b2.c')->get();
Last updated 2 years ago.

@jawb it looks like your code is not available anymore, can you post it again?


Sign in to participate in this thread!


Your banner here too?

jawb jawb Joined 16 Apr 2014


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.