Editing user data - crud of users

Step 01: In UserController, adding edit() method 

public function edit( User $user ) {
	$meta_title = 'Edit User ' . $user->name;
	return view( 'users.edit', compact( 'meta_title', 'user' ) );
}

Step 02: Adding edit.blade.php under resources/view/users folder inside section blade directive extending app layout

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <h2 class="float-start">Edit User</h2>
            <div class="float-end">
                <a class="btn btn-sm btn-primary" href="{{ route('users.index') }}"> Back to user</a>
             </div> 
             <div class="clearfix"></div> 
             <div class="card "> 
                <div class="card-body">
                <form method="post" action="{{ route('users.update' , $user->id) }}" id="updatefrm" method="post">
                    @csrf
                    @method('PUT')
                    <div class="form-group mb-2">
                        <label for="title">Name</label>  <br>    
                        <input name="name" class="form-control" placeholder="Firstname and Lastname" 
                                            title="Full Name" maxlength="50" type="text" id="UserFirstname" value="{{ $user->name }}"/>
                        @if ($errors->has('name'))
                            <span class="text-danger">{{ $errors->first('name') }}</span>
                        @endif                                                                                                                                    
                    </div> 
                    <div class="form-group mb-2">
                        <label for="title">Password</label>  <br>    
                        <input name="password" class="form-control" placeholder="Password, must be between 5 - 20 characters" title="password" type="password" id="UserPassword"/>
                        @if ($errors->has('password'))
                            <span class="text-danger">{{ $errors->first('password') }}</span>
                        @endif                                                           
                    </div>
                    <div class="form-group mb-2">
                        <label for="title">Email</label>  <br>    
                        <input name="email" class="form-control" placeholder="Email, verification required" title="Email" 
                                            maxlength="255" type="email" id="UserEmail" value="{{ $user->email }}"/>
                        @if ($errors->has('email'))
                            <span class="text-danger">{{ $errors->first('email') }}</span>
                        @endif                                                           
                    </div>
                    <div class="form-group mb-2">
                        <label for="title">Role</label>  <br> 
                        <div class="ml-4">
                            <input  type="radio" name="role_id" {{ ($user->role_id == 1) ? 'checked="checked"': '' }} value ="1" class="form-check-input mb-2">
                            Admin
                            <div class="mb-2"></div>
                            <input type="radio" name="role_id" {{ ($user->role_id == 2) ? 'checked="checked"': '' }} value ="2" class="form-check-input">
                            Customer     
                        </div> 
                        @if ($errors->has('role_id'))
                            <span class="text-danger">{{ $errors->first('role_id') }}</span>
                        @endif                                                                             
                    </div>            
                    <input class="btn btn-sm btn-primary" class="form-control" type="submit"  name="action" value="Edit" >
                </form>	
                </div>
             </div>
        </div>
    </div>
</div> 

Step 03: Creating UpdateUserRequest.php using artisan command for validation of form fields

php artisan make:request UpdateUserRequest

Step 04: Adding validation rules like below making sure authorize function returns true

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateUserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [                         
            'name' => 'required|max:255',             
            'email' => 'required|email|max:255',             
            'password' => 'nullable|min:5|max:20',             
            'role_id' => 'required'                    
        ];
    }
}

Please also note nullable value for password rule. It means password validation will be checked only if it is not empty. It will not be checked otherwise.  Password field is also set not to be required like StoreUserRequest.

Step 05: Back in UserController adding update() method like below and keeping users to edit list page after editing with flash message at the top

//at the top 
use App\Http\Requests\StoreUserRequest;
use Illuminate\Support\Facades\Hash;

public function update( User $user, UpdateUserRequest $request ) {

	$data = [
		'name' => $request->name,
		'email' => $request->email,
		'role_id' => $request->role_id,
	];

	if( !empty($request->password) ) { // value of password field will be hashed if not empty
		$data['password'] = Hash::make($request->password);
	}

	$user->update($data);     
	
	return redirect()->route('users.edit',$user->id)->with('success', 'User edited successfully');
} 

Related Posts


Building mini ecommerce in Laravel

Listing rows of users - crud

Adding user data - crud of users

Deleting user data - crud of users

Listing rows of products - crud

Listing rows of profiles - crud

Listing rows of orders - crud

Listing rows of order items - crud