Select Laravel Collective does not select value

Asked

Viewed 180 times

0

I’m trying to recover the value set in the bank and it stay selected in the edit form. The value 2 is coming up to 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']) !!}
  • It would not be enough to put the 2 (bank value) where it is null in the definition of select?

  • If you put 2 it is "No" but I am not able to make work the check so that if 1 is selected 1 otherwise select 2.

  • And how is the database value coming? Is it in some variable? It can only be 1 or 2, or it may not have value either?

  • The value 2 is coming in the variable $Coupon->cupUnic It will only have the value 1 or 2 (Yes and No)

  • So if instead of null you put the variable, Sim will be selected from variable 1 and Não will be if the variable is 2. That’s not what you did?

  • I have a field called Unique value that accepts two choices Yes (1) and No (2), the user selects an option (Not for example) but when going to the edit form I want to return the option that the user chose, in the case "No", but it is returning null. I want to do a check to fill in the amount that is coming from the bank.

  • How confusing. Who is returning null? The amount that comes from the database? In question you said that the "2" would be properly coming up to the form, so I believe it is not, but a field of type select has no value null. It’s really hard to understand your problem.

  • This is a form to register and edit, in case the value is null pq the user will choose the value, after choosing and going to the edit form has q come from the bank the option q he set.

Show 4 more comments

1 answer

1


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

$coupon->cupUnic ?? null

In your case, the Form::select would be:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.