Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

frezno's avatar
Level 36

Populate Select field from database

Is it possible to get data from the dabase and populate a select field with them?

Whatever i tried so far, it didn't work, ie i do get the data but Nova doesn't exept them, it doesn't like the format or whatever. I'm pretty clueless right now.

Just wanted to ask whether it's possible at all before posting a snippet for possible help from you guys.

0 likes
11 replies
jlrdw's avatar

In laravel there have been post and replies of how to do it. So maybe you could get a few ideas of how it's done in laravel and or eloquent. I suspect then it would work for nova.

frezno's avatar
Level 36

No problem with 'pure' Laravel, @jlrdw just Nova seems not to like me in that case

jlrdw's avatar

Link what you need to a laravel routine. Nova was meant as an admin panel to sit on top of Laravel, so just use the two together as needed.

In other words don't try to do everything in nova, that's not the intent of Nova.

Also see https://laracasts.com/series/laravel-nova-mastery/episodes/3

Nova uses laravel resources, nova is a front to have actions to do certain things with laravel models, etc.

frezno's avatar
Level 36

i guess i found it out meanwhile

frezno's avatar
Level 36

sure thing, @vlachos - you have to use pluck() for collecting the data.

frezno's avatar
frezno
OP
Best Answer
Level 36

i don't use any package, @vlachos so i'm not sure whether that's the same approach the package guys use.

Anyways it should look somewhat like this:

Select::make('User')->options(\App\User::pluck('name', 'id'))
8 likes
jdc1898's avatar

This is a great reply! I too searched for this a while back and @frezno post was spot on.

patgilmour's avatar

Laravel 6 with Nova 2 seems to require a slightly different approach.

Using a model Type, I had to do the following to get the Select menu to populate with the values from the table:

Select::make('Type')->options(function () {
    return array_filter(Element::pluck('name', 'id')->toArray());
}),
1 like

Please or to participate in this conversation.