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?