Getting selected values from a multiple select form in Laravel
Asked Answered
C

4

12

For generating a drop-down list with an item selected by default, the following is done:

echo Form::select('size', array('L' => 'Large', 'M' => 'Medium', 'S' => 'Small'), 'S');

So I generated a drop-down list that has more than one item selected by default, in the following way:

echo Form::select('size', array('L' => 'Large', 'M' => 'Medium', 'S' => 'Small'), array('S', 'M'), array('multiple'));

But how do I get the more than one selected values?

Input::get('size') returns only the last selected string.

Coombs answered 12/3, 2013 at 10:5 Comment(0)
W
36

First, if you want to have multiple item selected by default, you have to give an array of values as 3rd parameter, not a simple value.

Exemple:

Form::select('size', array('L' => 'Large', 'M' => 'Medium', 'S' => 'Small'), array('S', 'M'), array('multiple'));

should show the select with S and M selected.

For the second point, you should try to give a name like size[] instead of size, it could be solve the problem (because your posted select is not a simple value, its an array of values)

Walloon answered 12/3, 2013 at 10:36 Comment(1)
Thanks for the answer. Using size[] instead of size did what I had intended.Coombs
M
14

Usual Select statements go

<select name="select_name" id="select_name" multiple="multiple">

And the workflow is that Laravel gets the form elements by their name. To make it work, change the name to an array.

<select name="select_name[]" id="select_name" multiple="multiple">

This will make laravel get the values of select as an array of data.

Maryn answered 1/3, 2016 at 11:39 Comment(0)
A
1

According with https://laravelcollective.com/docs/5.2/html#drop-down-lists

Form::select('size[]',['L' => 'Large', 'M' => 'Medium', 'S' => 'Small'], ['S', 'M'], ['multiple' => 'multiple', 'class' => 'form-control']);

By the way, please notice dropdown's name (size[]) if you want to be able to use this field as array in your backend.

Things get tricky when you want uses relationships as value, for instance

models
user =>  common fields
size => id, name, slug  [
                         {id : 1 , name : Large, slug : L}, 
                         {id : 2 , name : Small, slug : S},
                         {id : 3 , name : Medium, slug : M}
                         ] 

user_size => id, user_id, size_id  [
                                     {id :1, user_id:1, size_id:1}
                                     {id :2, user_id:1, size_id:3}
                                   ]   

So $user->colors will return something like

laravel collection

[
  USER_SIZE => [  'user_id' => 1 , size_id' => 1 ],
  USER_SIZE => [  'user_id' => 1, 'size_id' => 3 ]
]

You could do something like, remember User Model have a sizes relationship of one to many with SIZE Model

Form::select('size[]',['L' => 'Large', 'M' => 'Medium', 'S' => 'Small'], $user->sizes->pluck('size')->pluck('slug')->toArray(), ['multiple' => 'multiple', 'class' => 'form-control']);

Hope it helps

Ava answered 20/9, 2016 at 15:26 Comment(0)
J
0

Right word is

pluck

In controller:

$skills = Skill::pluck('name', 'id');

$selectedSkills = $user->skills()->pluck('skill_user.id');

In blade:

Form::select('skills[]', $skills, $selectedSkills, ['class' => 'form-control', 'multiple' => 'multiple']) }}
Jahn answered 19/4, 2020 at 23:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.