我有以下代码从一个表中动态生成复选框:
{!! Form::open(['url' =>URL::to('admin/user/'.$profileUser->id) , 'method'=>'PUT', 'enctype'=>'multipart/form-data' ]) !!}
@foreach ($userRoles as $key => $userRole)
<tr>
<td width="10%">{{ Form::label($userRole->name) }}</td>
<td>{{ Form::checkbox( 'rolename[]', $userRole->id) }}</td>
</tr>
@endforeach
<tr>{{ Form::submit('Save', array('class' => 'btn', 'name' => 'roleSubmit')) }}</tr>
</tbody>
{{ Form::close() }}
在控制器中我有:
if (isset($_POST['roleSubmit'])){
DB::table('role_users')->where('user_id', $request->get('id', $id))->delete();
$userRole = json_encode($request->input('rolename'));
DB::table('role_users')->insert(
array(
'user_id' => $id,
'role_id' => $userRole
)
);
return redirect()->back();
}
然后我需要将复选框值存储在&#39; role_users&#39;像这样:
我搞乱它的任何准则?
答案 0 :(得分:1)
试一试。
if (isset($_POST['roleSubmit'])){
DB::table('role_users')->where('user_id', $request->get('id', $id))->delete();
$userRole = $request->input('rolename');
foreach($userRole as $role){
DB::table('role_users')->insert(
array(
'user_id' => $id,
'role_id' => $role
)
);
}
return redirect()->back();
}
每2次请求
控制器
$RoleByUser = DB::table('role_users')
->where('user_id', $user_id)
->get()
->pluck('role_id')
->ToArray();
刀片
{{ Form::checkbox( 'rolename[]', $userRole->id, (in_array($userRole->id, $RoleByUser)) ? true : false) }}