Home > Software design >  insert multiselect values into database from a foreach table
insert multiselect values into database from a foreach table

Time:04-13

enter image description here

I have the table above whereby at the last column is a multi select dropdown where one can select multiple number of countries. i have a problem in inserting the select values of each row into the table my code to inser is as below

<div >
<table id="table" >
<thead style="background-color:#00BFFF;">
<tr>
<th  scope="col"> No.</th>
<th  scope="col">Price List</th>
<th scope="col">Level One</th>
<th scope="col">Level Two</th>
<th scope="col">Level Three</th>
<th scope="col">Level Four</th>
<th scope="col">Level Five</th>
<th scope="col">Level Six</th>
<th scope="col">Level Seven</th>
<th scope="col">Level Eight</th>
<th scope="col">Level Nine</th>
<th scope="col">Level Ten</th>
<th width='150px' scope="col">Assigned Country</th>

</tr>
</thead>
<tbody>
<?php $i = 1 ?>
@foreach ($pricing as $price)
<tr>
<td >{{  $i  }}</td>
<input type="hidden" name="price_list[]" value="{{$price->id}}">
<td >{{$price->type_name}}</td>
<td><input type="checkbox"  name="level_one[]"  value="1"><input type="hidden"  name="level_one[]"  value="0"></td>
<td><input type="checkbox"  name="level_two[]"  value="1"><input type="hidden"  name="level_two[]"  value="0"></td>
<td><input type="checkbox"  name="level_three[]"  value="1"><input type="hidden"  name="level_three[]"  value="0"></td>
<td><input type="checkbox"  name="level_four[]"  value="1"><input type="hidden"  name="level_four[]"  value="0"></td>
<td><input type="checkbox"  name="level_five[]"  value="1"><input type="hidden"  name="level_five[]"  value="0"></td>
<td><input type="checkbox"  name="level_six[]"  value="1"><input type="hidden"  name="level_six[]"  value="0"></td>
<td><input type="checkbox"  name="level_seven[]"  value="1"><input type="hidden"  name="level_seven[]"  value="0"></td>
<td><input type="checkbox"   name="level_eight[]"  value="1"><input type="hidden"  name="level_eight[]"  value="0"></td>
<td><input type="checkbox"  name="level_nine[]"  value="1"><input type="hidden"  name="level_nine[]"  value="0"></td>
<td><input type="checkbox"  name="level_ten[]"  value="1"><input type="hidden"  name="level_ten[]"  value="0"></td>
<td>
<select id="country_id" name="country_id[{{$loop->index}}][]"   multiple="multiple">
@foreach ($countrys as $country)
<option value="{{$country->id}}">{{$country->country_name}}</option>
@endforeach  
</select>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</form>  
</div>

enter image description here

i want to end up with something like below in the database.The issue is getting multiselect values of each row

<td><input type="checkbox"  name="level_one[]"  value="1"><input type="hidden"  name="level_one[]"  value="0"></td>
<td><input type="checkbox"  name="level_two[]"  value="1"><input type="hidden"  name="level_two[]"  value="0"></td>
<td><input type="checkbox"  name="level_three[]"  value="1"><input type="hidden"  name="level_three[]"  value="0"></td>
<td><input type="checkbox"  name="level_four[]"  value="1"><input type="hidden"  name="level_four[]"  value="0"></td>
<td><input type="checkbox"  name="level_five[]"  value="1"><input type="hidden"  name="level_five[]"  value="0"></td>
<td><input type="checkbox"  name="level_six[]"  value="1"><input type="hidden"  name="level_six[]"  value="0"></td>
<td><input type="checkbox"  name="level_seven[]"  value="1"><input type="hidden"  name="level_seven[]"  value="0"></td>
<td><input type="checkbox"   name="level_eight[]"  value="1"><input type="hidden"  name="level_eight[]"  value="0"></td>
<td><input type="checkbox"  name="level_nine[]"  value="1"><input type="hidden"  name="level_nine[]"  value="0"></td>
<td><input type="checkbox"  name="level_ten[]"  value="1"><input type="hidden"  name="level_ten[]"  value="0"></td>
<td>
<select id="country_id" name="country_id[]"  multiple="multiple">
@foreach ($countrys as $country)
<option value="{{$country->id}}">{{$country->country_name}}</option>
@endforeach  
</select>
</td>

CodePudding user response:

Use implode to convert an array [1, 2, 3, 4,] into a string "1,2,3,4"


'country_id'=> implode(',', $request->input('country_id')[$i]??[]),

Your select input need to have a name attribute value ending with [] for it to be sent as an array.

example

<select name="country_id[1][]">
    //.....
</select>

EDIT:

Set your select input as this

@foreach ($pricing as $price)
<tr>
<td >{{$loop->iteration}}</td>
<input type="hidden" name="price_list[{{$loop->index}}]" value="{{$price->id}}">
<td >{{$price->type_name}}</td>
<td><input type="checkbox"  name="level_one[{{$loop->index}}]"  value="1"><input type="hidden"  name="level_one[{{$loop->index}}]"  value="0"></td>
<td><input type="checkbox"  name="level_two[{{$loop->index}}]"  value="1"><input type="hidden"  name="level_two[{{$loop->index}}]"  value="0"></td>
<td><input type="checkbox"  name="level_three[{{$loop->index}}]"  value="1"><input type="hidden"  name="level_three[{{$loop->index}}]"  value="0"></td>
<td><input type="checkbox"  name="level_four[{{$loop->index}}]"  value="1"><input type="hidden"  name="level_four[{{$loop->index}}]"  value="0"></td>
<td><input type="checkbox"  name="level_five[{{$loop->index}}]"  value="1"><input type="hidden"  name="level_five[{{$loop->index}}]"  value="0"></td>
<td><input type="checkbox"  name="level_six[{{$loop->index}}]"  value="1"><input type="hidden"  name="level_six[{{$loop->index}}]"  value="0"></td>
<td><input type="checkbox"  name="level_seven[{{$loop->index}}]"  value="1"><input type="hidden"  name="level_seven[{{$loop->index}}]"  value="0"></td>
<td><input type="checkbox"   name="level_eight[{{$loop->index}}]"  value="1"><input type="hidden"  name="level_eight[{{$loop->index}}]"  value="0"></td>
<td><input type="checkbox"  name="level_nine[{{$loop->index}}]"  value="1"><input type="hidden"  name="level_nine[{{$loop->index}}]"  value="0"></td>
<td><input type="checkbox"  name="level_ten[{{$loop->index}}]"  value="1"><input type="hidden"  name="level_ten[{{$loop->index}}]"  value="0"></td>
<td>
<select id="country_id" name="country_id[{{$loop->index}}][]"   multiple="multiple">
@foreach ($countrys as $country)
<option value="{{$country->id}}">{{$country->country_name}}</option>
@endforeach  
</select>
</td>
</tr>
@endforeach

CodePudding user response:

I was able to solve this this worked for me

  $this-> validate($request,[
            'user_id'=>'required',
           
        ]); 
        $count = count($request->input('price_list',[]));
                     
       
        $permission = new Permission;
        for ($i=0; $i<$count; $i  ){
        // $country_id =$request->input('country_id',[])[$i];
        
        // print_r(implode(',', $request->input('country_id')));
       
        
             
        $data[] = array(
        'user_id' =>$request->input('user_id'),
        'price_list'=>$request->input('price_list')[$i], 
        'level_one'=>$request->input('level_one')[$i], 
        'level_two'=>$request->input('level_two')[$i],
        'level_three'=>$request->input('level_three')[$i],
        'level_four'=>$request->input('level_four')[$i], 
        'level_five'=>$request->input('level_five')[$i],
        'level_six'=>$request->input('level_six')[$i],
        'level_seven'=>$request->input('level_seven')[$i], 
        'level_eight'=>$request->input('level_eight')[$i],  
        'level_nine'=>$request->input('level_nine')[$i],  
        'level_ten'=>$request->input('level_ten')[$i], 
        'country_id'=> implode(',', $request->input('country_id',[])[$i]),
        'created_at'=>Carbon::now(),     

         );
         }
           
        // dd($data);
           
        Permission::insert($data);
         return redirect()->back()->with('success', 'Permissions Created Successfully'); 

  • Related