cac

[HELP] Querying with groupby

Posted 3 months ago by cac

Models and relationship

ModelA
ModelB

ModelA belongs to ModelB
ModelB has many ModelA

Controller

$arrayModelA = ModelA::where('some_fk',$id)->get();
$groupedModelA = arrayModelA->groupBy('model_b_id')

Result

[
model_b_id => ModelACollection[]
some_other_model_b_id => ModelACollection[]
]

Desired result

[
SomeModelBObject => ModelACollection[]
SomeOtherModelBObject => ModelACollection[]
]

My grouped query is working fine grouping it by the model_b_id. The problem is that it doesn't bring the modelB object instead it groups simply by the field id. I need to have the entire object.

How can I achieve this?

Please sign in or create an account to participate in this conversation.