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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!