Laravel 8 User Roles and Permissions Tutorial Example

In this tutorial you will learn about the Laravel 8 User Roles and Permissions Tutorial Example and its application with practical example.

In this Laravel 8 user roles and permissions tutorial example I will show you how to implement user roles and permissions system in laravel 8 spatie/laravel-permission package. In this tutorial you will learn to create user roles and permission in laravel 8 application. We will be using spatie/laravel-permission package to create user roles and permission in laravel 8 application. The Spatie role permission package provide way to implement acl in laravel 8 application. With spatie we can create different user roles, user levels with specific permission.  With spatie we can assign user role, assign permission to user and assign permission to roles. In this example we will be using bootstrap auth scaffolding for login, register, logout, forget password, profile and reset password page.

Laravel 8 User Roles and Permissions Tutorial Example

In this step by step tutorial I will demonstrate you how to create or assign different user roles and user permission using laravel Spatie package. Please follow instructions given below:

Step 1 – Install Laravel 8 App

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

Step 2 – Database Configuration

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Step 3 – Install Spatie Composer Packages

In this step, we will install spatie/laravel-permission Package via the composer dependency manager. Use the following command to install spatie/laravel-permission Package.

After Installing Image intervention package, we need to add service provider in config/app.php file as following.

Now, run the following command to publish spatie package dependencies:

Step 4 – Create Model and Migration

Now, in this step we will create model and migration file. Please run the following command:

Now, open migration file name create_products_tables.php and add the follwoing code into it:


Now, put the following code into Product.php file:


Then, put the following code into User.php:

Step 5 – Register Middleware

Now we will register a middleare as following

Step 6 – Create Routes

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Step 7 – Create Controllers

Now, lets create some controller using command given below –

Then open UserController.php file and add the following code into it, which is placed on app/http/controllers directory:


Then open RoleController.php file and add the following code into it, which is placed on app/http/controllers directory:


Then open ProductController.php file and add the following code into it, which is placed on app/http/controllers directory:

Step 8 – Install Laravel UI

In this step we will install laravel UI using following command:

Step 9 – Install Bootstrap Auth Scaffolding

Now, we will install bootstrap auth scaffolding using following command:

Step 10 – Install Npm Packages

Now install npm dependencies using following command:

Now, open terminal and type the following command on terminal to create tables in database:

Step 11 – Create Blade Views

Now, In this step we will create following blade views file:

  • Layout
    • app.blade.php
  • Users Module
    • index.blade.php create.blade.php edit.blade.php show.blade.php
  • Roles Module
    • index.blade.php create.blade.php edit.blade.php show.blade.php
  • Product Module
    • index.blade.php create.blade.php edit.blade.php show.blade.php

Now, create and open app.blade.php file and add the following code into it, which is places inside resources/views/layouts directory:

Then create users directory inside resources/views directory.

Now, create and open index.blade.php file and add the following code into it, which is placed on resources/views/users directory:

Now, create and open create.blade.php file and add the following code into it, which is placed on resources/views/users directory:


Now, create and open edit.blade.php file and add the following code into it, which is placed on resources/views/users directory:


Now, create and open show.blade.php file and add the following code into it, which is placed on resources/views/users directory:



Now, create Roles directory inside resources/views directory.

Now, create and open index.blade.php file and add the following code into it, which is placed on resources/views/roles directory:

Now, create and open create.blade.php file and add the following code into it, which is placed on resources/views/roles directory:


Now, create and open edit.blade.php file and add the following code into it, which is placed on resources/views/roles directory:


Now, create and open show.blade.php file and add the following code into it, which is placed on resources/views/roles directory:


Then create Product directory inside resources/views directory.

Now, create and open index.blade.php file and add the following code into it, which is placed on resources/views/product directory: