Issue
I have a dual list box in my Laravel form and I want to get those values that are taken over to the right side. Currently, only 1 value gets taken over, but if a user picks 3 values over, I would like all 3 values to be stored in the database. Is there any way to do this?
My controller:
public function storesurvey(Request $request)
{
$energy = new Customer();
$energy->rank1 = $request->input('rank1');
$energy->comments = $request->input('comments');
$energy->save();
return redirect('/survey')->with('success', 'data added');
}
My view:
<div class="container">
<div class="jp-multiselect">
<div class="from-panel">
<select name="from[]" class="form-control" size="8" multiple="multiple">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
<option value="4">Item 4</option>
<option value="5">Item 5</option>
</select>
</div>
<div class="move-panel">
<button type="button" class="btn-move-all-right btn-primary"></button>
<button type="button" class="btn-move-selected-right"></button>
<button type="button" class="btn-move-all-left"></button>
<button type="button" class="btn-move-selected-left"></button>
</div>
<div class="to-panel">
<select name="rank1" class="form-control" size="8" multiple="multiple">
</select>
</div>
</div>
<script>
$(".jp-multiselect").jQueryMultiSelection();
</script>
<hr />
<!-- 2 -->
</div>
Could someone explain why only one value gets taken over and how I can send all value that are present on the right.
Solution
To send multiple value from a select input (or checkboxs having the same name) you need to name the input with []
like you did in the select form[]
<select name="rank1[]" class="form-control" size="8" multiple="multiple">
Then in your controller, you need to treat it as an array.
Here i used a json incode to convert all rank1 values into a single value string of format json.
public function storesurvey(Request $request)
{
$energy = new Customer();
$energy->rank1 = json_encode($request->input('rank1'));
$energy->comments = $request->input('comments');
$energy->save();
return redirect('/survey')->with('success', 'data added');
}
Other Solution
You can also make this auto using Casting
-Customer.php
class Customer .....
{
protected $casts = [
'rank1' => 'array'
];
....
}
Then in your controller, you can assign an array to the attribute rank1
directly without converting it
public function storesurvey(Request $request)
{
$energy = new Customer();
$energy->rank1 = $request->input('rank1');
$energy->comments = $request->input('comments');
$energy->save();
return redirect('/survey')->with('success', 'data added');
}
Answered By - N69S
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.