😁Laravel 8 User Roles & Permissions and Product CRUD With Images spatie/laravel-permission Full (ok)

https://ksharing.info/laravel-8-user-roles-permissions-and-product-crud-with-images/

Đọc thêm

Xem cách xác thực bằng can, bài này xác thực bằng permission

public function __construct()
  {
    $this->middleware('can:user list', ['only' => ['index', 'show']]);
    $this->middleware('can:user create', ['only' => ['create', 'store']]);
    $this->middleware('can:user edit', ['only' => ['show', 'edit']]);
    $this->middleware('can:user delete', ['only' => ['destroy']]);
  }

Sử dụng app.blade.php này để không bị lỗi giao diện bootstrap

C:\xampp82\htdocs\testcom\resources\views\layouts\app.blade.php

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="">
  <meta name="author" content="">
  <title>{{ config('app.name') }}</title>
  <!-- Bootstrap core CSS -->
  @vite(['resources/sass/app.scss', 'resources/js/app.js'])
  <!-- Custom styles for this template -->
  <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
  <!-- Static navbar -->
  <nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
    <div class="container">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
          aria-expanded="false" aria-controls="navbar">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="{{ url('/') }}">Laravel 8 User Roles & Permissions and Product CRUD With Images
          Tutorial</a>
      </div>
      <div id="navbar" class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
          <li class="active"><a href="{{ url('/') }}">Home</a></li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
          <!-- Authentication Links -->
          @guest
          <li><a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a></li>
          <li><a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a></li>
          @else
          <li><a class="nav-link" href="{{ route('users.index') }}">Manage Users</a></li>
          <li><a class="nav-link" href="{{ route('roles.index') }}">Manage Role</a></li>
          <li><a class="nav-link" href="{{ route('products.index') }}">Manage Product</a></li>
          <li class="dropdown">
            <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-bs-toggle="dropdown" aria-expanded="false">{{ Auth::user()->name }}<span class="caret"></span></a>
            <ul  class="dropdown-menu" aria-labelledby="dropdownMenuLink">
              <li class="dropdown-item"><a href="{{ route('users.show', Auth::id()) }}">Profile</a></li>
              <li class="dropdown-item">
                <a href="{{ route('logout') }}" onclick="event.preventDefault();
                                                     document.getElementById('logout-form').submit();">
                  {{ __('Logout') }}
                </a>
                <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                  @csrf
                </form>
              </li>
            </ul>
          </li>
          @endguest
        </ul>
      </div>
      <!--/.nav-collapse -->
    </div>
  </nav>
  <div class="container">
    @yield('content')
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</body>
</html>

Laravel 8 User Roles & Permissions and Product CRUD With Images Tutorial

CRUD is an acronym that comes from the world of computer programming and refers to the four functions that are considered necessary to implement a persistent storage application: create, read, update and delete.

User management is important feature in any web application. A user should have access to the permissions that only required. That way, user can only see the menus that assigned to its role.

In this article, we are using spatie github package for roles and permissions in laravel 8 application. just follow bellow step to create acl in laravel 8.

Spatie role permission composer package provide way to create acl in laravel 8. they provide how to assign role to user, how to assign permission to user and how to assign permission assign to roles. i will write step by step creating roles and permissions in laravel 8 application.

Step 1: Create Laravel application

we need to create a new laravel application. for that open your terminal or command prompt and create a new Laravel application.

And than go to project directory

Step 2: Install require composer package

Now we nedd to install require Spatie package for ACL (Access Control List). Open your terminal and execute bellow command.

Now we need to register provider class to config/app.php file so open file and add service provider.

If we want to change on Spatie package then we need to run bellow command that will generate config/permission.php and migration files

Step 3: Configure database connection

go to .env file and confiure database connection type, db host, db port, db database, db username, db password

now we can run migrate command

Step 4: Create product migration

we need to create product table for migration as using bellow command. This will generate CreateProductsTable class at database>migrations

products table:

Step 5: Create model

In this step, we we will create a product model to get data from product table. Run the below artisan command to generate product model. That will generate Product model at app>Models

add fillable attribute in Product model

Laravel have already User model. We just need to add HasRoles trait in it. So open User model and add it.

Step 6: Register middlewares package

The package will handle roles and permission middlewares. We can add them inside app/Http/Kernel.php file.

Step 7: Create authentication scaffold

Now we need to implement Laravel default authentication feature. So run the following command in Terminal.

We need views for login and register. We can do it with following command. This will generate view file for login and register.

user roles & permissions

Now we also need to compile view assets for authentication view. So run the below npm command.

Note: The below npm commands only require for asset compiling, if you are adding your own view, then you don’t need to run npm commands.

Step 8: Create controllers

We are going to build CRUD part for users, roles and product table, so we need resource controllers for them. Run below commands one by one to create controllers.

app/Http/Controllers/UserController.php

app/Http/Controllers/RoleController.php

app/Http/Controllers/ProductController.php

Step 9: Create routes

We have created all of controllers. Now we need to create routes for them, open routes/web.php and add new routes in it.

Step 10: Create view files

In this is step, we are going to create a blade view files. We will use one common layout and extend it to all views. Here is the list of all views that we will create in resources/views/ directory. You can go to all folder and copy each view to your local file. we need to create following files

Let’s start from common layout file. It is already there, you just slightly need to change in it and add route link in navigation bar.

resources/views/layouts/app.blade.php

resources/views/home.blade.php

User roles & permissions
User roles & permissions

complete users view balde file form https://github.com/robin-bd/laravel-8-CRUD-with-images/tree/main/resources/views/users

complete roles view balde file form https://github.com/robin-bd/laravel-8-CRUD-with-images/tree/main/resources/views/roles

Laravel CRUD
Product CRUD

complete products view balde file form https://github.com/robin-bd/laravel-8-CRUD-with-images/tree/main/resources/views/products

Step 11: Create Seeder

In this step we will create seeder for permissions, Right now we have fixed permission so we create using seeder as listed bellow, but if you can add more permission as you want:

And put bellow code in PermissionTableSeeder seeder this way:

database/seeds/PermissionTableSeeder.php

database/seeds/CreateAdminUserSeeder.php

update DatabaseSeeder class at

database/seeds/CreateAdminUserSeeder.php

run bellow code for database table migrate, seeding and run application server.

default email and password for admin access

This full application are available on github. you can download it and run it. https://github.com/robin-bd/laravel-8-CRUD-with-images

Share this:

Like this:

How to Clear Laravel Cache Application, route, config and viewAugust 4, 2019In "Laravel Framework"

The Software Testing Life CycleNovember 12, 2022In "Software Testing"

Definition of Virtual MachineFebruary 23, 2021In "Cloud Computing"

Noor E Alam Robin 2Tags :Laravel 8

Post navigation

First Image from MarsWindows 11 versus Windows 10: Every distinction in Microsoft’s new OS

2 thoughts on “Laravel 8 User Roles & Permissions and Product CRUD With Images Tutorial”

    • Author gravatarAuthor gravatarPedroMay 10, 2022, 11:39 pm

      Thanks for share. It is excellent. I have a question: if You create a role, then for link it to Permissions and authorize, you must do it writting code or it is authomatic? Thank you for your time

Last updated

Was this helpful?