Joel Leger
Joel Leger

Reputation: 3

Pre-selecting values in a multi-select drop-down list in laravel

I'm relatively new to Laravel (and using Laravel 4), but been around PHP and C# a long time. Seems like this should be easy, but I can't find anywhere that tells me how to do this.

In my Controller I get the data from the database and send it to the view like this:

$sections = DB::table('paperSections')->lists('section','id');
return View::make('layouts.publisher.step2', array('sections' => $sections));

in my View, I have the following:

{{ Form::select('sections[]', $sections, '', array('multiple')) }}

which generates a select list like this:

<select multiple="multiple" id="sections" name="sections">
<option value="1">News</option>
<option value="2">Sports</option>
<option value="3">Features</option>
<option value="4">Arts and Entertainment</option>
<option value="5">Technology and Science</option>
<option value="6">Op-Ed</option>
</select>

Lets assume I have a string (e.g. "1,3,5") which represents the multiple options selected previously. How can I re-select those three options using that string?

Upvotes: 0

Views: 3306

Answers (1)

Jarek Tkaczyk
Jarek Tkaczyk

Reputation: 81147

Pass array of selected options as 3rd param:

$selected = explode(',', $idsAsString);

Form::select('sections[]', $sections, $selected, ['multiple'])

Upvotes: 3

Related Questions