Adding user data - crud of users

Step 01: Adding user add button just above of table element in list of users

<h2 class="float-start">All Users</h2>
<div class="float-end">
	<a class="btn btn-sm btn-primary" href="{{ route('users.create') }}"> Add User</a>
</div>  

Step 02: In UserController, adding create() method

public function create( ) {
	$meta_title = 'Add User';
	return view( 'users.create', compact( 'meta_title' ) );
} 

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

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <h2 class="float-start">Add 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.store') }}" id="updatefrm" method="post">
                    @csrf
                    <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="{{ old('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" required="required"/>
                        @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="{{ old('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" checked value ="1" class="form-check-input mb-2">
                            Admin
                            <div class="mb-2"></div>
                            <input type="radio" name="role_id" 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="Signup" >
                </form>	
                </div>
             </div>
        </div>
    </div>
</div> 

Step 04: Creating UserStoreRequest.php using artisan command for validation of form fields

php artisan make:request StoreUserRequest

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

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreUserRequest 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|unique:users',             
            'password' => 'required|min:5|max:20',             
            'role_id' => 'required'                    
        ];
    }
}

Step 06: Changing User.php model like below adding role_id in the fillable array element

protected $fillable = [
	'name',
	'email',
	'password',
	'role_id'
];

Step 07: Back in UserController adding store() method like below and redirecting to users list page after user addition

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

public function store( StoreUserRequest $request ) {
	$user = User::create([
		'name' => $request->name,
		'email' => $request->email,
		'password' => Hash::make($request->password),
		'role_id' => $request->role_id,
	]);     
	
	//dd($user);
	return redirect()->route('users.index');
} 

Related Posts


Building mini ecommerce in Laravel

Listing rows of users - crud

Editing 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