Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Nova: Default Select Option From BelongsTo Field

I need to show selected country in the Belongs To Fields is there any method in which i can pass the value and it would be selected by default.

like image 963
Nabeel Qadri Avatar asked Jan 14 '19 13:01

Nabeel Qadri


2 Answers

From this issue on Github, I tried the following in one of my BelongsTo relationship,

BelongsTo::make('countries')
    ->withMeta([
        'belongsToId' => 1 // default value for the select
    ]);

.. and it worked perfectly.

like image 94
Mozammil Avatar answered Nov 18 '22 05:11

Mozammil


You want to make sure that the value will be set correctly when editing so use the following code:

BelongsTo::make('countries')
    ->withMeta([
        'belongsToId' => $this->countries_id ?? 1 // default value for the select
    ]);
like image 29
ykay says Reinstate Monica Avatar answered Nov 18 '22 06:11

ykay says Reinstate Monica