Use of Multiselect in HTML/ Laravel
1. Use Select2 css files.
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
Here, I'm using the cdn link for select 2. If your work is offline, save the css.
2. Use Select2 js files.
Here, I'm again using the cdn for select2 js.
3. Identify the field where multiselect is needed. In my case, I am using multiselect to use multiple size of clothes, i.e. Small, Medium, Large.
For this, we have the html as:
<div class="select-multiple form-control" multiple="multiple"> Free Size Small Medium Large Extra Large
The main thing to consider is to use array in name and id. Here, I have used name=”select” and id=”select”. This is because of the multiple data to be inserted in the same name.
4. Default select value.
Take a look at above class name I have used, i.e. select-multple. You can use any name. Then use this name to call the following js.
5. Just make sure you are using the jQuery plugin.
6. You are all set to use the multi select option and send the data.
7. For the controller part to receive the data,
$multipleVal = $request->get('select');
$item->size = serialize($multipleVal);
Here, select is the name of the html name. I am using serialize() method to store the values in the database of $item->size. To retrieve database data, just use the unserialize() method.