Laravel 9

Install Laravel Breeze for Authentication Using Laravel 11

In this tutorial ,i am to going teach the Laravel Breeze.Laravel Breeze provides a simple way to implementation of Laravel’s authentication features.

  1. Run the following commands to install Laravel Breeze
composer require laravel/breeze --dev
php artisan breeze:install
npm install
npm run dev
php artisan migrate
php artisan make:migration add_role_to_users_table --table=users

 

above the code snippt sets up basic authentication functionality, including login and registration.

2. Create Role Field in Users Table

To differentiate between admin and regular users, add a role field to the users table. Run the following migration to add the field.

Run the following command to create a migration

php artisan make:migration add_role_to_users_table --table=users

In the migration file, update the up() function to add the role column:

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->string('role')->default('user'); // Role field, default is 'user'
    });
}

Then, run the migration:

php artisan migrate

3. Modify User Model

In the User.php model, add a method to check if a user is an admin or a regular user

class User extends Authenticatable
{
    
    // Check if the user is an admin
    public function isAdmin()
    {
        return $this->role === 'admin';
    }
}

4. Protect Routes Based on Role

In the routes/web.php file, define separate routes for the admin and user functionalities.

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;

// Admin Routes
Route::middleware(['auth', 'isAdmin'])->group(function () {
    Route::get('/admin', [HomeController::class, 'adminDashboard'])->name('admin.dashboard');

});

// User Routes
Route::middleware(['auth'])->group(function () {
    Route::get('/user', [HomeController::class, 'userDashboard'])->name('user.dashboard');

});

5. Create Middleware for Admin Role

To restrict access to admin routes, create a middleware that checks if the user is an admin.

Run the following command:

php artisan make:middleware IsAdmin

In the app/Http/Middleware/IsAdmin.php file, update the handle() method to check the user’s role.

public function handle($request, Closure $next)
{
    if (auth()->check() && auth()->user()->isAdmin()) {
        return $next($request);
    }

    return redirect('/user'); // Redirect non-admins to user dashboard
}

6. Modify the HomeController

Create a HomeController with methods to serve both admin and user dashboards.

php artisan make:controller HomeController
class HomeController extends Controller
{
    // Admin Dashboard
    public function adminDashboard()
    {
        return view('admin.dashboard'); // Admin view
    }

    // User Dashboard
    public function userDashboard()
    {
        return view('user.dashboard'); // User view
    }
}

7. Create Views for Admin and User Dashboards

In the resources/views folder, create separate folders for admin and user views.

  • resources/views/admin/dashboard.blade.php
@extends('layouts.app')

@section('content')
<div class="container">
    <h1>Admin Dashboard</h1>
    <p>Welcome to the admin dashboard!</p>
</div>
@endsection
  • resources/views/user/dashboard.blade.php

 

admin

Recent Posts

Employee Working Hours Calculation System using C#.net

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

6 days ago

Java Payroll System Calculate Employee Overtime

Act as a Java developer to create a program that calculates the gross wage for…

1 week ago

Employee Working Hours Calculation System using Java

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

1 week ago

Laravel 11 School Management System

In this tutorial, we will teach you how to create a simple school management system…

2 weeks ago

How to Make Admin Panel Using React MUI

I have design the Admin Basic templete using React MUI Design Admin Dashboard and Login.Here…

4 weeks ago

Point of Sales (POS) System Documentation

this tutorial we will discuss about how to make the point of sales system step…

1 month ago