duanchigeng4313 2017-09-07 04:20
浏览 115
已采纳

Laravel表单不会更新数据库中的值而是删除它们

I have form where I can change user role. When I try to change and hit Save button I've got invalid foreach() argument error and previous record in database for this user is deleted completely.

This is my form

{!! Form::model($user, ['method' => 'PATCH','route' => ['admin.addper', $user->user_id]]) !!}
<div class="row">
    <div class="col-md-4 col-xs-12">
         <div class="form-group">
             <label for="title" class="control-block">Username:</label>
                {{ Form::text('username', $user->username, ['class' => 'form-control', 'disabled']) }}
         </div>
    </div><!-- end col-4/12 -->
    <div class="col-xs-12 col-md-8">
         <div class="form-group">
            <label for="title" class="control-block">Choose which Role you want to assign to user:</label><br>
                @foreach($roles as $value)
                    {{ Form::checkbox('roles', $value->id, in_array($value->id, $userRole) ? true : false, array('class' => 'name')) }}
                        <strong>{{ $value->display_name }}</strong> 
                        <br/>
                @endforeach
         </div>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12 text-center">
            <button type="submit" class="btn btn-primary">Submit</button>
    </div>
</div>
{!! Form::close() !!}

And this is the controller part

public function update(Request $request, $id)
{
    $this->validate($request, [
        'roles' => 'required'
    ]);

    $input = $request->all();
    $user = User::find($id);
    $user->update($input);
    DB::table('role_user')->where('user_id',$id)->delete();

    foreach ($request->input('roles') as $key => $value) {
        $user->attachRole($value);
    }

    return redirect()->route('users')
                    ->with('success','User Role Updated Successfully');
}

The error is on the foreach in the controller

ErrorException: Invalid argument supplied for foreach()

Since I'm sure that I pass correct user_id and correct value of role_id why is this error?

dd($request->input('roles'));

return correct id which I'm choose on checkbox.

  • 写回答

4条回答 默认 最新

  • dongmou3615 2017-09-07 04:32
    关注

    You need to delete the existing role and attach the new one like this:

    DB::table('role_user')->where('user_id',$id)->delete();
    $user->attachRole($request->input('roles'));
    

    I think you have single role in the role_user table for each user, so you can use find and update method as well.

    Like,

    DB::table('role_user')
       ->where('user_id',$id)
       ->update(['role' => $request->input('roles')]);
    

    This way will reduce extra overhead of unnecessary delete and with just single query you can update role of the user.

    I hope you have understood.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用