Support the ongoing development of Laravel.io →
Database Eloquent

I have a query that I was previously calling with Quote::with(['detail.orders'])->findOrFail($id);

Which works just fine, however, the orders table now has a column 'is_primary' that I wish to split the response on, (i.e, select where is_primary = 'true').

With this new column, I wish to build a query that would generate a response like;

{
	"detail": {
		"primary_orders": [{...}],
		"secondary_orders": [{...}]
	}
}

Is this possible through the query builder?

Thanks

(Postgresql 9.3)

Edit :

I do have this;

Quote::with([
                'detail.primaryOrders' => function($query) {
                    $query->where('is_primary', '=', 'true');
                },
                'detail.secondaryOrders' => function($query) {
                    $query->where('is_primary', '=', 'false');
                }])->findOrFail($id);

But as with many things related to Eloquent, my understanding of the builder is limited and I'm not sure if this is entirely optimal

Last updated 3 years ago.
0

Maybe a model resource could come in handy here ? It may allow you to make different requests for primary and secondary but let you put them in one response

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Peter Lockett cerwym Joined 11 Mar 2019

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.

© 2025 Laravel.io - All rights reserved.