Skip to content
Advertisement

Laravel 7 – Edit and update value of checkbox in form

I want to uncheck / check the checkbox for ‘active’ users based on the value set in the form. In MySQL database, I added an ‘active’ column of type ‘tinyint (1)’ in table ‘users’. I have a form to edit different values ​​of the users, like name, email, etc. When I submit the form, everything updates fine except the checkbox, so … I don’t understand how to make sure that if it’s not checked the value changes to a 0.

  • I have this in my UserController:
    /**
         * Show the form for editing the specified resource.
         *
         * @param  AppUser  $user
         * @return IlluminateHttpResponse
         */
        public function edit($userID)
        {
            //
            $user = User::query()->findOrFail($userID);
            $roles = Role::pluck('role_name','id');
            $departments = Department::all(['id','department_name']);
    
            return view('user.edit',compact('user','roles','departments'));
        }
    
        /**
         * Update the specified resource in storage.
         *
         * @param  IlluminateHttpRequest  $request
         * @param  AppUser  $user
         * @return IlluminateHttpResponse
         */
        public function update(Request $request, $usuarioID)
        {
            $user = User::query()->findOrFail($usuarioID);
    
            $user->update($request->only('name','email','password', 'roles', 'departament_id', 'active'));
            $user->roles()->sync($request->roles);
    
            return back()->with('Success','User updated successfully');
        }

  • And this is in my edit view
 <form method="POST" action="{{route('User.update', $user->id)}}" class="user" enctype="multipart/form-data"">
    {!! method_field('PUT') !!}

    @csrf

    @include('user.form')
    <!--Roles-->
    <br>
    <div class=" row justify-content-center text-center">

    <div class="col-md-6">
        <h5 class="txt-unam text-center text-primary">@lang('edit.Roles')</h5>
        @foreach($roles as $id => $name)
        <input type="checkbox" value="{{$id}}" {{$user->roles->pluck('id')->contains($id) ? 'checked' : ''}} name="roles[]">
        {{ $name }}
        @endforeach
    </div>

    <div class="col-md-6">
        Active <input type="checkbox" name="active" value="1" {{$user->active ? 'checked' : ''}}>
    </div>
    </div>
    <br>
    <div class="text-center">

        <button class="btn btn-warning" type="submit" value="Update">@lang('edit.Update')</button>
        <button class="btn btn-danger" onclick="cancel()" value="Cancel">@lang('edit.Cancel')</button>
    </div>
</form>

The checkbox for roles is working fine, but the ‘active’ checkbox is not… The value stays in 1 and I don’t know why. Can someone help me?

Advertisement

Answer

When your checkbox is not checked, you don’t have any value in Request. So you can check that with isset() function in update method like this:

public function update(Request $request, $usuarioID)
{
    $input = $request->only('name','email','password', 'roles', 'departament_id', 'active');
    if(!isset($request->active))
        $input['active'] = false;
    
    $user = User::query()->findOrFail($usuarioID);

    $user->update($input);
    $user->roles()->sync($request->roles);

    return back()->with('Success','User updated successfully');
}

Also, make sure you added ‘active’ field in your User.php model protected $fillable.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement