Subquery on relationship

I'm trying to do some aggregation with relationships to build an api response. Forms and Categories have many-to-many in both directions.

This work:

 $forms = $organisation->forms()->with('categories')->get()->toArray();

And it gives me a nice array with all the related category models. But I would like to make a subquery to make the categories be just a flat array with the id's.

So I'm trying to figure out how to do this:

 $forms = $organisation->forms()->with('categories')->[ pluck('categories.id')->toArray() ]->get()->toArray();

That doesn't work of course. But hopefully it explains what I'm after. So I want to do an eager loading of the categories relationship, but I only want to fetch the id's of the categories - not the whole model.

Is that even doable?



from Recent Questions - Stack Overflow https://ift.tt/2NVFT99
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)