Support the ongoing development of Laravel.io →
Eloquent

In Laravel all model values returns as simple list. Is it possible somehow set up primary key as array key? For example for model I do it like this:

Models\Category\Filter::where('category_id', '=', $category_id)->with('values.filters.values')->get()->keyBy('id')->toArray()

But I also need that values.filters.values would use their id as key.

Last updated 3 years ago.
0

Try this in your model: protected $primaryKey = ' YourArrayVal ' ;

Hope it helps :)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.