0

In my controller I have an array of options ($options=['a','b','c']) which I am using it in my view in a select input field. I need the values 'a','b','c' to be saved in the database instead of their corresponding keys (0,1,2). How can I do this? Controller:

public function create()
    {
        $options=['a','b','c'];
        return view('example.create', compact('options'));
    }

View:

<div class="form-group col-lg-4">
     {!! Form::select('option', $options ,null , ['class' => 'form-control']) !!}
</div>
1
  • 1
    Maybe you can show us some code? Commented Sep 1, 2015 at 19:41

1 Answer 1

2

If you're using the array to populate Form::select('selected_option', $options), you need it to look like this:

$options = ['a' => 'a', 'b' => 'b', 'c' => 'c'];

Then, when the form values are passed back to the controller, Input::get('selected_option') will receive the value 'a', 'b', or 'c'.

A really simple way to get the array you need is:

$options = array_combine($options, $options);, which will create an array with keys and values that are the same.

Sign up to request clarification or add additional context in comments.

Comments

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.