Back

Model split response on column true.


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

Cameron replied 2 months ago

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


Sign in to participate in this thread!



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