Laravel Nova select field default value
Asked Answered
U

3

11

How can i set a default value in Laravel Nova in a select field?

   Select::make('Car')->options([
        'mercedes' => 'Mercedes',
        'audi' => 'Audi',
        'bmw' => 'BMW',
    ]),

enter image description here

https://nova.laravel.com/docs/3.0/resources/fields.html#select-field

In this example i want that mercedes is the default value.

Uniformitarian answered 26/10, 2021 at 7:58 Comment(0)
O
10

You can use default function

Select::make('Car')->options([
    'mercedes' => 'Mercedes',
    'audi' => 'Audi',
    'bmw' => 'BMW',
])->default('bmw'),
Odious answered 27/10, 2021 at 11:54 Comment(0)
U
8

I found out in case someone has the same problem.

 Select::make('Car')
    ->options([
        'mercedes' => 'Mercedes',
        'audi' => 'Audi',
        'bmw' => 'BMW',
    ])
    ->withMeta(['value' => 'mercedes']),
Uniformitarian answered 26/10, 2021 at 10:4 Comment(2)
But this may override the saved data from the database.Levileviable
This does override saved data.Lambdoid
B
0

If you are using enum on your database

Select::make('Car')->options([
    'mercedes' => 'Mercedes',
    'audi' => 'Audi',
    'bmw' => 'BMW',
])->default(0),
Bonaparte answered 12/11, 2021 at 5:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.