2

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?

1 Answer 1

3

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'
];
Sign up to request clarification or add additional context in comments.

1 Comment

This works; I just felt there was an easier way to just get the values, rather than getting the keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.