从 foreach table 将多选值插入数据库
insert multiselect values into database from a foreach table
我有上面的 table,最后一列是一个多 select 下拉列表,其中可以 select 多个国家/地区。我在将每一行的 select 值插入 table 时遇到问题我要插入的代码如下
<div class="container-flow">
<table id="table" class="table table-hover table-bordered table-sm text-center">
<thead style="background-color:#00BFFF;">
<tr>
<th class="fixed" scope="col"> No.</th>
<th class="fixed" 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 class="fixed_td">{{ $i++}}</td>
<input type="hidden" name="price_list[]" value="{{$price->id}}">
<td class="fixed_td">{{$price->type_name}}</td>
<td><input type="checkbox" name="level_one[]" class="check" value="1"><input type="hidden" name="level_one[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_two[]" class="check" value="1"><input type="hidden" name="level_two[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_three[]" class="check" value="1"><input type="hidden" name="level_three[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_four[]" class=" check" value="1"><input type="hidden" name="level_four[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_five[]" class=" check" value="1"><input type="hidden" name="level_five[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_six[]" class="check" value="1"><input type="hidden" name="level_six[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_seven[]" class="check" value="1"><input type="hidden" name="level_seven[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_eight[]" class=" check" value="1"><input type="hidden" name="level_eight[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_nine[]" class="check" value="1"><input type="hidden" name="level_nine[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_ten[]" class="check" value="1"><input type="hidden" name="level_ten[]" class="check" value="0"></td>
<td>
<select id="country_id" name="country_id[{{$loop->index}}][]" class="form-control country_id" multiple="multiple">
@foreach ($countrys as $country)
<option value="{{$country->id}}">{{$country->country_name}}</option>
@endforeach
</select>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</form>
</div>
我想在 database.The 问题中得到如下所示的结果是获取每行的多个 select 值
<td><input type="checkbox" name="level_one[]" class="check" value="1"><input type="hidden" name="level_one[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_two[]" class="check" value="1"><input type="hidden" name="level_two[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_three[]" class="check" value="1"><input type="hidden" name="level_three[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_four[]" class=" check" value="1"><input type="hidden" name="level_four[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_five[]" class=" check" value="1"><input type="hidden" name="level_five[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_six[]" class="check" value="1"><input type="hidden" name="level_six[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_seven[]" class="check" value="1"><input type="hidden" name="level_seven[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_eight[]" class=" check" value="1"><input type="hidden" name="level_eight[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_nine[]" class="check" value="1"><input type="hidden" name="level_nine[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_ten[]" class="check" value="1"><input type="hidden" name="level_ten[]" class="check" value="0"></td>
<td>
<select id="country_id" name="country_id[]" class="form-control country_id" multiple="multiple">
@foreach ($countrys as $country)
<option value="{{$country->id}}">{{$country->country_name}}</option>
@endforeach
</select>
</td>
使用 implode 将数组 [1, 2, 3, 4,]
转换为字符串 "1,2,3,4"
'country_id'=> implode(',', $request->input('country_id')[$i]??[]),
您的 select 输入需要以 []
结尾的名称属性值才能作为数组发送。
例子
<select name="country_id[1][]">
//.....
</select>
编辑:
将您的select输入设置为
@foreach ($pricing as $price)
<tr>
<td class="fixed_td">{{$loop->iteration}}</td>
<input type="hidden" name="price_list[{{$loop->index}}]" value="{{$price->id}}">
<td class="fixed_td">{{$price->type_name}}</td>
<td><input type="checkbox" name="level_one[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_one[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_two[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_two[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_three[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_three[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_four[{{$loop->index}}]" class=" check" value="1"><input type="hidden" name="level_four[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_five[{{$loop->index}}]" class=" check" value="1"><input type="hidden" name="level_five[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_six[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_six[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_seven[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_seven[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_eight[{{$loop->index}}]" class=" check" value="1"><input type="hidden" name="level_eight[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_nine[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_nine[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_ten[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_ten[{{$loop->index}}]" class="check" value="0"></td>
<td>
<select id="country_id" name="country_id[{{$loop->index}}][]" class="form-control country_id" multiple="multiple">
@foreach ($countrys as $country)
<option value="{{$country->id}}">{{$country->country_name}}</option>
@endforeach
</select>
</td>
</tr>
@endforeach
我能够解决这个问题,这对我有用
$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');
我在尝试更新复选框时遇到另一个问题
<td><input type="checkbox" name="level_one[]" class="check" value="1" {{($perm->level_one == 1 ? ' checked' : '') }}><input type="hidden" name="level_one[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_two[]" class="check" value="1" {{($perm->level_two == 1 ? ' checked' : '') }}><input type="hidden" name="level_two[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_three[]" class="check" value="1"{{($perm->level_three == 1 ? ' checked' : '') }}><input type="hidden" name="level_three[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_four[]" class=" check" value="1"{{($perm->level_four == 1 ? ' checked' : '') }} ><input type="hidden" name="level_four[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_five[]" class=" check" value="1" {{($perm->level_five == 1 ? ' checked' : '') }}><input type="hidden" name="level_five[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_six[]" class="check" value="1" {{($perm->level_six == 1 ? ' checked' : '') }}><input type="hidden" name="level_six[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_seven[]" class="check" value="1" {{($perm->level_seven == 1 ? ' checked' : '') }}><input type="hidden" name="level_seven[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_eight[]" class=" check" value="1" {{($perm->level_eight == 1 ? ' checked' : '') }}><input type="hidden" name="level_eight[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_nine[]" class="check" value="1" {{($perm->level_nine == 1 ? ' checked' : '') }}><input type="hidden" name="level_nine[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_ten[]" class="check" value="1" {{($perm->level_ten == 1 ? ' checked' : '') }}><input type="hidden" name="level_ten[]" class="check" value="0" ></td>
隐藏输入不是工作问题我怎么能在没有隐藏输入的情况下工作,因为当我像检查一样编辑下面的十列[![在此处输入图像描述] [1]] [1]
当该列的 dd($data) 我应该得到 111111111 但当我删除隐藏时我得到 0101010101 它工作正常但如果没有勾选一个复选框会给出偏移错误
我的更新控制器是这样的
public function update(Request $request, $id)
{
$this-> validate($request,[
'user_id'=>'required',
]);
$ids = $request->input('id',[]);
foreach($ids as $key => $value){
$permission = Permission::find($request->id[$key]);
$permission->level_one = $request->level_one[$key];
$permission->level_two = $request->level_two[$key];
$permission->level_three = $request->level_three[$key];
$permission->level_four = $request->level_four[$key];
$permission->level_five = $request->level_five[$key];
$permission->level_six = $request->level_six[$key];
$permission->level_seven = $request->level_seven[$key];
$permission->level_eight = $request->level_eight[$key];
$permission->level_nine = $request->level_nine[$key];
$permission->level_ten = $request->level_ten[$key];
$permission->country_id =implode(',', $request->input('country_id',[])[$key]??[]);
// print($permission);
$permission->save();
}
// return($permission);
// print($permission);
return redirect()->back()->with('success', 'Permissions Updated Successfully');
我应该更改什么才能使更新正常工作
[1]: https://i.stack.imgur.com/L8vIb.png
我有上面的 table,最后一列是一个多 select 下拉列表,其中可以 select 多个国家/地区。我在将每一行的 select 值插入 table 时遇到问题我要插入的代码如下
<div class="container-flow">
<table id="table" class="table table-hover table-bordered table-sm text-center">
<thead style="background-color:#00BFFF;">
<tr>
<th class="fixed" scope="col"> No.</th>
<th class="fixed" 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 class="fixed_td">{{ $i++}}</td>
<input type="hidden" name="price_list[]" value="{{$price->id}}">
<td class="fixed_td">{{$price->type_name}}</td>
<td><input type="checkbox" name="level_one[]" class="check" value="1"><input type="hidden" name="level_one[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_two[]" class="check" value="1"><input type="hidden" name="level_two[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_three[]" class="check" value="1"><input type="hidden" name="level_three[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_four[]" class=" check" value="1"><input type="hidden" name="level_four[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_five[]" class=" check" value="1"><input type="hidden" name="level_five[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_six[]" class="check" value="1"><input type="hidden" name="level_six[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_seven[]" class="check" value="1"><input type="hidden" name="level_seven[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_eight[]" class=" check" value="1"><input type="hidden" name="level_eight[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_nine[]" class="check" value="1"><input type="hidden" name="level_nine[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_ten[]" class="check" value="1"><input type="hidden" name="level_ten[]" class="check" value="0"></td>
<td>
<select id="country_id" name="country_id[{{$loop->index}}][]" class="form-control country_id" multiple="multiple">
@foreach ($countrys as $country)
<option value="{{$country->id}}">{{$country->country_name}}</option>
@endforeach
</select>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</form>
</div>
我想在 database.The 问题中得到如下所示的结果是获取每行的多个 select 值
<td><input type="checkbox" name="level_one[]" class="check" value="1"><input type="hidden" name="level_one[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_two[]" class="check" value="1"><input type="hidden" name="level_two[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_three[]" class="check" value="1"><input type="hidden" name="level_three[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_four[]" class=" check" value="1"><input type="hidden" name="level_four[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_five[]" class=" check" value="1"><input type="hidden" name="level_five[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_six[]" class="check" value="1"><input type="hidden" name="level_six[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_seven[]" class="check" value="1"><input type="hidden" name="level_seven[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_eight[]" class=" check" value="1"><input type="hidden" name="level_eight[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_nine[]" class="check" value="1"><input type="hidden" name="level_nine[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_ten[]" class="check" value="1"><input type="hidden" name="level_ten[]" class="check" value="0"></td>
<td>
<select id="country_id" name="country_id[]" class="form-control country_id" multiple="multiple">
@foreach ($countrys as $country)
<option value="{{$country->id}}">{{$country->country_name}}</option>
@endforeach
</select>
</td>
使用 implode 将数组 [1, 2, 3, 4,]
转换为字符串 "1,2,3,4"
'country_id'=> implode(',', $request->input('country_id')[$i]??[]),
您的 select 输入需要以 []
结尾的名称属性值才能作为数组发送。
例子
<select name="country_id[1][]">
//.....
</select>
编辑:
将您的select输入设置为
@foreach ($pricing as $price)
<tr>
<td class="fixed_td">{{$loop->iteration}}</td>
<input type="hidden" name="price_list[{{$loop->index}}]" value="{{$price->id}}">
<td class="fixed_td">{{$price->type_name}}</td>
<td><input type="checkbox" name="level_one[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_one[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_two[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_two[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_three[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_three[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_four[{{$loop->index}}]" class=" check" value="1"><input type="hidden" name="level_four[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_five[{{$loop->index}}]" class=" check" value="1"><input type="hidden" name="level_five[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_six[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_six[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_seven[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_seven[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_eight[{{$loop->index}}]" class=" check" value="1"><input type="hidden" name="level_eight[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_nine[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_nine[{{$loop->index}}]" class="check" value="0"></td>
<td><input type="checkbox" name="level_ten[{{$loop->index}}]" class="check" value="1"><input type="hidden" name="level_ten[{{$loop->index}}]" class="check" value="0"></td>
<td>
<select id="country_id" name="country_id[{{$loop->index}}][]" class="form-control country_id" multiple="multiple">
@foreach ($countrys as $country)
<option value="{{$country->id}}">{{$country->country_name}}</option>
@endforeach
</select>
</td>
</tr>
@endforeach
我能够解决这个问题,这对我有用
$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');
我在尝试更新复选框时遇到另一个问题
<td><input type="checkbox" name="level_one[]" class="check" value="1" {{($perm->level_one == 1 ? ' checked' : '') }}><input type="hidden" name="level_one[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_two[]" class="check" value="1" {{($perm->level_two == 1 ? ' checked' : '') }}><input type="hidden" name="level_two[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_three[]" class="check" value="1"{{($perm->level_three == 1 ? ' checked' : '') }}><input type="hidden" name="level_three[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_four[]" class=" check" value="1"{{($perm->level_four == 1 ? ' checked' : '') }} ><input type="hidden" name="level_four[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_five[]" class=" check" value="1" {{($perm->level_five == 1 ? ' checked' : '') }}><input type="hidden" name="level_five[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_six[]" class="check" value="1" {{($perm->level_six == 1 ? ' checked' : '') }}><input type="hidden" name="level_six[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_seven[]" class="check" value="1" {{($perm->level_seven == 1 ? ' checked' : '') }}><input type="hidden" name="level_seven[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_eight[]" class=" check" value="1" {{($perm->level_eight == 1 ? ' checked' : '') }}><input type="hidden" name="level_eight[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_nine[]" class="check" value="1" {{($perm->level_nine == 1 ? ' checked' : '') }}><input type="hidden" name="level_nine[]" class="check" value="0"></td>
<td><input type="checkbox" name="level_ten[]" class="check" value="1" {{($perm->level_ten == 1 ? ' checked' : '') }}><input type="hidden" name="level_ten[]" class="check" value="0" ></td>
我的更新控制器是这样的
public function update(Request $request, $id)
{
$this-> validate($request,[
'user_id'=>'required',
]);
$ids = $request->input('id',[]);
foreach($ids as $key => $value){
$permission = Permission::find($request->id[$key]);
$permission->level_one = $request->level_one[$key];
$permission->level_two = $request->level_two[$key];
$permission->level_three = $request->level_three[$key];
$permission->level_four = $request->level_four[$key];
$permission->level_five = $request->level_five[$key];
$permission->level_six = $request->level_six[$key];
$permission->level_seven = $request->level_seven[$key];
$permission->level_eight = $request->level_eight[$key];
$permission->level_nine = $request->level_nine[$key];
$permission->level_ten = $request->level_ten[$key];
$permission->country_id =implode(',', $request->input('country_id',[])[$key]??[]);
// print($permission);
$permission->save();
}
// return($permission);
// print($permission);
return redirect()->back()->with('success', 'Permissions Updated Successfully');
我应该更改什么才能使更新正常工作 [1]: https://i.stack.imgur.com/L8vIb.png