z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.15k stars 2.82k forks source link

Set value to save of a select field #5686

Open moham96 opened 1 year ago

moham96 commented 1 year ago

Description:

I have a select field that I would like to set it's value to a pre-defined one and make it read-only, this is possible with text fields but not select fields. How can I select a specific option in a select field?

Regards

optiktr commented 1 year ago

if you want you can use $form-hidden field or $form->saving(function{ //your codes });

moham96 commented 1 year ago

if you want you can use $form-hidden field or $form->saving(function{ //your codes });

Maybe I didn't phrase it correctly, what I meant is to show it to the user, not set it during saving.

if I use $form->text('dep', 'department')->readonly()->value($department); then it shows the value of $department which is expected:

Screenshot 2022-11-25 at 10 02 41 PM

but in the case of a select you may have an array of value=>name like:

$options=[
'engineer'=>'engineering department',
]

so I want to show the user the select menu and choose a specific option(show the name and set the value.

The only workaround I can think of is using a text field and setting it to the name of the option and then overriding it in 'saving' to the value, but it seems hacky. and would prefer to show a select menu and not a text field. Regards

optiktr commented 1 year ago

İnspect to this bro. $myDBTable=DB::select('select * from sigortalars'); foreach ($myDBTable as $row) {$myArrayData[$row->id] = $row->sigorta_adi; } $form->select('user', __('Choose User'))->options($myArrayData);

optiktr commented 1 year ago

Yo dont forget import use DB;