Use keyBy('name') on eager-loaded relation

Posted 8 months ago by Lars-Janssen


I've got this in my controller:

return Port::filter($filters)

Scores relation looks like this:

public function scores()
    return $this->hasMany(Score::class)
        ->select("id", "port_id", "name", DB::raw('AVG(score) as score'))
        ->groupBy('port_id', 'name');

Eager loaded response is this:

How do I rename the array keys 0 and 1 to the properties ("COSTS" and "SAIL_BOAT_FRIENDLY") ?


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

Reply to

Use Markdown with GitHub-flavored code blocks.