Select Laravel Collective does not select value

0

I'm trying to retrieve the value set in the database and it gets selected in the edit form. The value 2 is reaching the form but I'm not getting it selected.

{!! Form::label('cupUnic', 'Único?') !!}
{!! Form::select('cupUnic', ['' => 'Selecione', '1' => 'Sim', '2' => 'Não'], 'null', ['class'=>'form-control', 'parsley-trigger'=>'change', 'required'=>'required']) !!}
    
asked by anonymous 12.05.2017 / 02:46

1 answer

1

As discussed in chat , you will be using the same form on both the registration page and the edit page. On the registration page, you want the third parameter of Form::select to be null , because the user has not yet selected any value. Already on the edit page, you want this third parameter to be the value of $coupon->cupUnic , which is the value stored in the database. Considering that you are using PHP 7, as also said in chat, you can use the null coalescer operator to return the value of the variable, if it is set, or null otherwise.

$coupon->cupUnic ?? null

In your case, Form::select would be:

{!! Form::select('cupUnic', ['' => 'Selecione', '1' => 'Sim', '2' => 'Não'], $coupon->cupUnic ?? null, ['class'=>'form-control', 'parsley-trigger'=>'change', 'required'=>'required']) !!}
    
12.05.2017 / 03:34