Home > OS >  How can you get selected dual list box values in Laravel
How can you get selected dual list box values in Laravel

Time:01-06

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 >                
                  <div >
                      <div >
                          <select name="from[]"  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 >
                          <button type="button" ></button>
                          <button type="button" ></button>
                          <button type="button" ></button>
                          <button type="button" ></button>
                      </div>
                      <div >
                          <select name="rank1"  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.

CodePudding user response:

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[]"  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');
}
  •  Tags:  
  • Related