Many Rodríguez
Many Rodríguez

Reputation: 65

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.

    /**
         * Show the form for editing the specified resource.
         *
         * @param  \App\User  $user
         * @return \Illuminate\Http\Response
         */
        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  \Illuminate\Http\Request  $request
         * @param  \App\User  $user
         * @return \Illuminate\Http\Response
         */
        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');
        }

 <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?

Upvotes: 0

Views: 2347

Answers (2)

Chando
Chando

Reputation: 434

For checkbox is unchecked $request->only(... ,'active') The active key is not exists in array. So in update method is be like this.

$user->update(['name' => $request->name, 'email' => $request->email, 'password' => $request->password, 'roles' => $request->roles, 'department_id' => $request->department_id])

That is why is not updated.

You can try this.

$data = $request->only('name','email','password', 'roles', 'department_id');
$data['active'] = $request->get('active',0); // if checkbox is unchecked is 0 instead of null    
$user->update($data);

Upvotes: 0

Mostafa Kalantari Fard
Mostafa Kalantari Fard

Reputation: 304

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.

Upvotes: 0

Related Questions