Skip to content
Advertisement

Getting Values from Laravel drop-down form

I have a simple drop-down in a form.

The form is:

{{Form::label('language', 'Language')}}
{{Form::select('Language', $language, '', ['class'=>'form-control']) }}

In the view function in the controller the array is:

$languages = ['English', 'French'];

The store function in the controller is:

$language = $request->input('language');

The method, however, stores the position of the values instead of the value itself. So, if I do a dd(request()->all());, I get:

array:4 [▼
"_token" => "..."
"title" => "Course"
"Language" => "1"]

How can I get the values instead of the position?

Advertisement

Answer

Since you are receiving the array key to a specific array element, you just need to make sure you can access that array to retrieve the value when needed.

$languages[$request->input('language')] // if key = 1 would give you "French" (should check validity of key using array_key_exists)

OR

You can change your array to define keys as you want them so that when you use Form::select it uses the keys you specified as the html element value.

Your array have to look like this then:

$languages = [
    'english' => 'English', 
    'french' => 'French'
];
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement