Laravel 5.8 User Roles and Permissions Tutorial

Laravel 5.8 User Roles and Permissions Tutorial

In this post we will give you information about Laravel 5.8 User Roles and Permissions Tutorial. Hear we will give you detail about Laravel 5.8 User Roles and Permissions TutorialAnd how to use it also give you demo for it if it is necessary.

In this tutorial, i want to share with you how to build roles and permissions on laravel 5.8 application. you can do it acl in laravel 5.8 using spatie composer package. i will explain how to implement User Roles and Permissions(ACL) using spatie/laravel-permission composer package.

Spatie role permission composer package provide way to create acl in laravel 5.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 5.8 application.

I also posted on tutorial for ACL User Roles and Permissions using entrust package, you can see here : Laravel 5 – User Roles and Permissions (ACL) using entrust package.

Roles and Permissions through you can create several types of users with different role and permission, i mean some user have only see listing of items module, some user can also edit items modules, for delete and etc.

In this examples i created three modules as listed bellow:

User Management

Role Management

Product Management

After register user, you don’t have any roles, so you can edit your details and assign admin role to you from User Management. After that you can create your own role with permission like role-list, role-create, role-edit, role-delete, product-list, product-create, product-edit, product-delete. you can check with assign new user and check that.

You need to just follow few step and you will get full example of ACL:

Step 1: Laravel 5.8 Installation

We are going from scratch so, If you haven’t installed laravel in your system then you can run bellow command and get fresh Laravel project.

composer create-project --prefer-dist laravel/laravel blog

Step 2: Install Composer Packages

Now we require to install Spatie package for ACL, that way we can use it’s method. Also we will install form collection package. So Open your terminal and run bellow command.

composer require spatie/laravel-permission

composer require laravelcollective/html

Now open config/app.php file and add service provider and aliase.

config/app.php

'providers' => [

....

SpatiePermissionPermissionServiceProvider::class,

CollectiveHtmlHtmlServiceProvider::class,

],

'aliases' => [

....

'Form' => CollectiveHtmlFormFacade::class,

'Html' => CollectiveHtmlHtmlFacade::class,

],

We can also custom changes on Spatie package, so if you also want to changes then you can fire bellow command and get config file in config/permission.php.

php artisan vendor:publish --provider="SpatiePermissionPermissionServiceProvider" --tag="config"

Also see:Laravel 5.8 CRUD (Create Read Update Delete) Tutorial For Beginners

Step 3: Create Migrations

In this step we have to create three migrations for as listed bellow tables:

1. users

2. products

3. roles

4. permissions

5. model_has_permissions

6. model_has_roles

7. role_has_permissions

So, if you install fresh project then you have already users table migration but if you don’t have products table, so can create manually and other table can create using Spatie package command, so run bellow command and check migration file also.

php artisan vendor:publish --provider="SpatiePermissionPermissionServiceProvider" --tag="migrations"

php artisan make:migration create_products_table

users table:

<?php


use IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


class CreateUsersTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('users', function (Blueprint $table) {

$table->bigIncrements('id');

$table->string('name');

$table->string('email');

$table->string('password');

$table->rememberToken();

$table->timestamps();

});

}


/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('users');

}

}

products table:

<?php


use IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


class CreateProductsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('products', function (Blueprint $table) {

$table->bigIncrements('id');

$table->string('name');

$table->text('detail');

$table->timestamps();

});

}


/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('products');

}

}

Spatie tables:

<?php


use IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


class CreatePermissionTables extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

$tableNames = config('permission.table_names');


Schema::create($tableNames['permissions'], function (Blueprint $table) {

$table->increments('id');

$table->string('name');

$table->string('guard_name');

$table->timestamps();

});


Schema::create($tableNames['roles'], function (Blueprint $table) {

$table->increments('id');

$table->string('name');

$table->string('guard_name');

$table->timestamps();

});


Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames) {

$table->integer('permission_id')->unsigned();

$table->morphs('model');


$table->foreign('permission_id')

->references('id')

->on($tableNames['permissions'])

->onDelete('cascade');


$table->primary(['permission_id', 'model_id', 'model_type']);

});


Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames) {

$table->integer('role_id')->unsigned();

$table->morphs('model');


$table->foreign('role_id')

->references('id')

->on($tableNames['roles'])

->onDelete('cascade');


$table->primary(['role_id', 'model_id', 'model_type']);

});


Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {

$table->integer('permission_id')->unsigned();

$table->integer('role_id')->unsigned();


$table->foreign('permission_id')

->references('id')

->on($tableNames['permissions'])

->onDelete('cascade');


$table->foreign('role_id')

->references('id')

->on($tableNames['roles'])

->onDelete('cascade');


$table->primary(['permission_id', 'role_id']);


app('cache')->forget('spatie.permission.cache');

});

}


/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

$tableNames = config('permission.table_names');


Schema::drop($tableNames['role_has_permissions']);

Schema::drop($tableNames['model_has_roles']);

Schema::drop($tableNames['model_has_permissions']);

Schema::drop($tableNames['roles']);

Schema::drop($tableNames['permissions']);

}

}

Now run migration:

php artisan migrate

Step 4: Create Models

In this step we have to create model for User and Product table, so if you get fresh project then you have User Model have so just replace code and other you should create.

app/User.php

<?php

namespace App;

use IlluminateNotificationsNotifiable;

use IlluminateContractsAuthMustVerifyEmail;

use IlluminateFoundationAuthUser as Authenticatable;

use SpatiePermissionTraitsHasRoles;

class User extends Authenticatable

{

use Notifiable;

use HasRoles;

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'name', 'email', 'password',

];

/**

* The attributes that should be hidden for arrays.

*

* @var array

*/

protected $hidden = [

'password', 'remember_token',

];

/**

* The attributes that should be cast to native types.

*

* @var array

*/

protected $casts = [

'email_verified_at' => 'datetime',

];

}

app/Product.php

<?php


namespace App;


use IlluminateDatabaseEloquentModel;


class Product extends Model

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'name', 'detail'

];

}

Step 5: Add Middleware

Spatie package provide it’s in-built middleware that way we can use it simply and that is display as bellow:

role

permission

So, we have to add middleware in Kernel.php file this way :

app/Http/Kernel.php

....

protected $routeMiddleware = [

....

'role' => SpatiePermissionMiddlewaresRoleMiddleware::class,

'permission' => SpatiePermissionMiddlewaresPermissionMiddleware::class,

]

....


Step 6: Create Authentication

In this step we require to create authentication of Laravel 5.8, so laravel provide artisan command to create authentication that way we don’t require to create route and controller for login and registration. so run bellow command:

php artisan make:auth

Step 7: Create Routes

We require to add number of route for users module, products module and roles module. In this this route i also use middleware with permission for roles and products route, so add route this way:

routes/web.php

Auth::routes();


Route::get('/home', 'HomeController@index')->name('home');


Route::group(['middleware' => ['auth']], function() {

Route::resource('roles','RoleController');

Route::resource('users','UserController');

Route::resource('products','ProductController');

});

Step 8: Add Controllers

In this step we have add three controller for users module, products module and roles module so you can create three controller like as bellow:

app/Http/Controllers/UserController.php

<?php


namespace AppHttpControllers;


use IlluminateHttpRequest;

use AppHttpControllersController;

use AppUser;

use SpatiePermissionModelsRole;

use DB;

use Hash;


class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function index(Request $request)

{

$data = User::orderBy('id','DESC')->paginate(5);

return view('users.index',compact('data'))

->with('i', ($request->input('page', 1) - 1) * 5);

}


/**

* Show the form for creating a new resource.

*

* @return IlluminateHttpResponse

*/

public function create()

{

$roles = Role::pluck('name','name')->all();

return view('users.create',compact('roles'));

}


/**

* Store a newly created resource in storage.

*

* @param IlluminateHttpRequest $request

* @return IlluminateHttpResponse

*/

public function store(Request $request)

{

$this->validate($request, [

'name' => 'required',

'email' => 'required|email|unique:users,email',

'password' => 'required|same:confirm-password',

'roles' => 'required'

]);


$input = $request->all();

$input['password'] = Hash::make($input['password']);


$user = User::create($input);

$user->assignRole($request->input('roles'));


return redirect()->route('users.index')

->with('success','User created successfully');

}


/**

* Display the specified resource.

*

* @param int $id

* @return IlluminateHttpResponse

*/

public function show($id)

{

$user = User::find($id);

return view('users.show',compact('user'));

}


/**

* Show the form for editing the specified resource.

*

* @param int $id

* @return IlluminateHttpResponse

*/

public function edit($id)

{

$user = User::find($id);

$roles = Role::pluck('name','name')->all();

$userRole = $user->roles->pluck('name','name')->all();


return view('users.edit',compact('user','roles','userRole'));

}


/**

* Update the specified resource in storage.

*

* @param IlluminateHttpRequest $request

* @param int $id

* @return IlluminateHttpResponse

*/

public function update(Request $request, $id)

{

$this->validate($request, [

'name' => 'required',

'email' => 'required|email|unique:users,email,'.$id,

'password' => 'same:confirm-password',

'roles' => 'required'

]);


$input = $request->all();

if(!empty($input['password'])){

$input['password'] = Hash::make($input['password']);

}else{

$input = array_except($input,array('password'));

}


$user = User::find($id);

$user->update($input);

DB::table('model_has_roles')->where('model_id',$id)->delete();


$user->assignRole($request->input('roles'));


return redirect()->route('users.index')

->with('success','User updated successfully');

}


/**

* Remove the specified resource from storage.

*

* @param int $id

* @return IlluminateHttpResponse

*/

public function destroy($id)

{

User::find($id)->delete();

return redirect()->route('users.index')

->with('success','User deleted successfully');

}

}

app/Http/Controllers/ProductController.php

<?php


namespace AppHttpControllers;


use AppProduct;

use IlluminateHttpRequest;


class ProductController extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

function __construct()

{

$this->middleware('permission:product-list|product-create|product-edit|product-delete', ['only' => ['index','show']]);

$this->middleware('permission:product-create', ['only' => ['create','store']]);

$this->middleware('permission:product-edit', ['only' => ['edit','update']]);

$this->middleware('permission:product-delete', ['only' => ['destroy']]);

}

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function index()

{

$products = Product::latest()->paginate(5);

return view('products.index',compact('products'))

->with('i', (request()->input('page', 1) - 1) * 5);

}


/**

* Show the form for creating a new resource.

*

* @return IlluminateHttpResponse

*/

public function create()

{

return view('products.create');

}


/**

* Store a newly created resource in storage.

*

* @param IlluminateHttpRequest $request

* @return IlluminateHttpResponse

*/

public function store(Request $request)

{

request()->validate([

'name' => 'required',

'detail' => 'required',

]);


Product::create($request->all());


return redirect()->route('products.index')

->with('success','Product created successfully.');

}


/**

* Display the specified resource.

*

* @param AppProduct $product

* @return IlluminateHttpResponse

*/

public function show(Product $product)

{

return view('products.show',compact('product'));

}


/**

* Show the form for editing the specified resource.

*

* @param AppProduct $product

* @return IlluminateHttpResponse

*/

public function edit(Product $product)

{

return view('products.edit',compact('product'));

}


/**

* Update the specified resource in storage.

*

* @param IlluminateHttpRequest $request

* @param AppProduct $product

* @return IlluminateHttpResponse

*/

public function update(Request $request, Product $product)

{

request()->validate([

'name' => 'required',

'detail' => 'required',

]);


$product->update($request->all());


return redirect()->route('products.index')

->with('success','Product updated successfully');

}


/**

* Remove the specified resource from storage.

*

* @param AppProduct $product

* @return IlluminateHttpResponse

*/

public function destroy(Product $product)

{

$product->delete();


return redirect()->route('products.index')

->with('success','Product deleted successfully');

}

}

app/Http/Controllers/RoleController.php

<?php


namespace AppHttpControllers;


use IlluminateHttpRequest;

use AppHttpControllersController;

use SpatiePermissionModelsRole;

use SpatiePermissionModelsPermission;

use DB;


class RoleController extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

function __construct()

{

$this->middleware('permission:role-list|role-create|role-edit|role-delete', ['only' => ['index','store']]);

$this->middleware('permission:role-create', ['only' => ['create','store']]);

$this->middleware('permission:role-edit', ['only' => ['edit','update']]);

$this->middleware('permission:role-delete', ['only' => ['destroy']]);

}


/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function index(Request $request)

{

$roles = Role::orderBy('id','DESC')->paginate(5);

return view('roles.index',compact('roles'))

->with('i', ($request->input('page', 1) - 1) * 5);

}


/**

* Show the form for creating a new resource.

*

* @return IlluminateHttpResponse

*/

public function create()

{

$permission = Permission::get();

return view('roles.create',compact('permission'));

}


/**

* Store a newly created resource in storage.

*

* @param IlluminateHttpRequest $request

* @return IlluminateHttpResponse

*/

public function store(Request $request)

{

$this->validate($request, [

'name' => 'required|unique:roles,name',

'permission' => 'required',

]);


$role = Role::create(['name' => $request->input('name')]);

$role->syncPermissions($request->input('permission'));


return redirect()->route('roles.index')

->with('success','Role created successfully');

}

/**

* Display the specified resource.

*

* @param int $id

* @return IlluminateHttpResponse

*/

public function show($id)

{

$role = Role::find($id);

$rolePermissions = Permission::join("role_has_permissions","role_has_permissions.permission_id","=","permissions.id")

->where("role_has_permissions.role_id",$id)

->get();


return view('roles.show',compact('role','rolePermissions'));

}


/**

* Show the form for editing the specified resource.

*

* @param int $id

* @return IlluminateHttpResponse

*/

public function edit($id)

{

$role = Role::find($id);

$permission = Permission::get();

$rolePermissions = DB::table("role_has_permissions")->where("role_has_permissions.role_id",$id)

->pluck('role_has_permissions.permission_id','role_has_permissions.permission_id')

->all();


return view('roles.edit',compact('role','permission','rolePermissions'));

}


/**

* Update the specified resource in storage.

*

* @param IlluminateHttpRequest $request

* @param int $id

* @return IlluminateHttpResponse

*/

public function update(Request $request, $id)

{

$this->validate($request, [

'name' => 'required',

'permission' => 'required',

]);


$role = Role::find($id);

$role->name = $request->input('name');

$role->save();


$role->syncPermissions($request->input('permission'));


return redirect()->route('roles.index')

->with('success','Role updated successfully');

}

/**

* Remove the specified resource from storage.

*

* @param int $id

* @return IlluminateHttpResponse

*/

public function destroy($id)

{

DB::table("roles")->where('id',$id)->delete();

return redirect()->route('roles.index')

->with('success','Role deleted successfully');

}

}

Step 9: Add Blade Files

This is last step we have to add numbers view for layouts, users module, roles module, products modules and errors page, so create number of view like as bellow:

resources/views/layouts/app.blade.php

<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">

<!-- CSRF Token -->

<meta name="csrf-token" content="{{ csrf_token() }}">

<title>{{ config('app.name', 'Laravel 5.8 User Roles and Permissions Tutorial') }}</title>

<!-- Scripts -->

<script src="{{ asset('js/app.js') }}" defer></script>

<!-- Fonts -->

<link rel="dns-prefetch" href="https://fonts.gstatic.com">

<link href="https://fonts.googleapis.com/css?family=Raleway:300,400,600" rel="stylesheet" type="text/css">

<!-- Styles -->

<link href="{{ asset('css/app.css') }}" rel="stylesheet">

</head>

<body>

<div id="app">

<nav >

<div >

<a href="{{ url('/') }}">

Laravel 5.8 User Roles and Permissions - ItSolutionStuff.com

</a>

<button type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">

<span ></span>

</button>


<div id="navbarSupportedContent">

<!-- Left Side Of Navbar -->

<ul ></ul>


<!-- Right Side Of Navbar -->

<ul >

<!-- Authentication Links -->

@guest

<li><a href="{{ route('login') }}">{{ __('Login') }}</a></li>

<li><a href="{{ route('register') }}">{{ __('Register') }}</a></li>

@else

<li><a href="{{ route('users.index') }}">Manage Users</a></li>

<li><a href="{{ route('roles.index') }}">Manage Role</a></li>

<li><a href="{{ route('products.index') }}">Manage Product</a></li>

<li >

<a id="navbarDropdown" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>

{{ Auth::user()->name }} <span ></span>

</a>


<div aria-labelledby="navbarDropdown">

<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>

</div>

</li>

@endguest

</ul>

</div>

</div>

</nav>


<main >

<div >

@yield('content')

</div>

</main>

</div>

</body>

</html>

resources/views/users/index.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<div >

<h2>Users Management</h2>

</div>

<div >

<a href="{{ route('users.create') }}"> Create New User</a>

</div>

</div>

</div>


@if ($message = Session::get('success'))

<div >

<p>{{ $message }}</p>

</div>

@endif


<table >

<tr>

<th>No</th>

<th>Name</th>

<th>Email</th>

<th>Roles</th>

<th width="280px">Action</th>

</tr>

@foreach ($data as $key => $user)

<tr>

<td>{{ ++$i }}</td>

<td>{{ $user->name }}</td>

<td>{{ $user->email }}</td>

<td>

@if(!empty($user->getRoleNames()))

@foreach($user->getRoleNames() as $v)

<label >{{ $v }}</label>

@endforeach

@endif

</td>

<td>

<a href="{{ route('users.show',$user->id) }}">Show</a>

<a href="{{ route('users.edit',$user->id) }}">Edit</a>

{!! Form::open(['method' => 'DELETE','route' => ['users.destroy', $user->id],'style'=>'display:inline']) !!}

{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}

{!! Form::close() !!}

</td>

</tr>

@endforeach

</table>


{!! $data->render() !!}


<p ><small>Tutorial by ItSolutionStuff.com</small></p>

@endsection

resources/views/users/create.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<div >

<h2>Create New User</h2>

</div>

<div >

<a href="{{ route('users.index') }}"> Back</a>

</div>

</div>

</div>


@if (count($errors) > 0)

<div >

<strong>Whoops!</strong> There were some problems with your input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif



{!! Form::open(array('route' => 'users.store','method'=>'POST')) !!}

<div >

<div >

<div >

<strong>Name:</strong>

{!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}

</div>

</div>

<div >

<div >

<strong>Email:</strong>

{!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}

</div>

</div>

<div >

<div >

<strong>Password:</strong>

{!! Form::password('password', array('placeholder' => 'Password','class' => 'form-control')) !!}

</div>

</div>

<div >

<div >

<strong>Confirm Password:</strong>

{!! Form::password('confirm-password', array('placeholder' => 'Confirm Password','class' => 'form-control')) !!}

</div>

</div>

<div >

<div >

<strong>Role:</strong>

{!! Form::select('roles[]', $roles,[], array('class' => 'form-control','multiple')) !!}

</div>

</div>

<div >

<button type="submit" >Submit</button>

</div>

</div>

{!! Form::close() !!}


<p ><small>Tutorial by ItSolutionStuff.com</small></p>

@endsection

resources/views/users/edit.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<div >

<h2>Edit New User</h2>

</div>

<div >

<a href="{{ route('users.index') }}"> Back</a>

</div>

</div>

</div>


@if (count($errors) > 0)

<div >

<strong>Whoops!</strong> There were some problems with your input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


{!! Form::model($user, ['method' => 'PATCH','route' => ['users.update', $user->id]]) !!}

<div >

<div >

<div >

<strong>Name:</strong>

{!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}

</div>

</div>

<div >

<div >

<strong>Email:</strong>

{!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}

</div>

</div>

<div >

<div >

<strong>Password:</strong>

{!! Form::password('password', array('placeholder' => 'Password','class' => 'form-control')) !!}

</div>

</div>

<div >

<div >

<strong>Confirm Password:</strong>

{!! Form::password('confirm-password', array('placeholder' => 'Confirm Password','class' => 'form-control')) !!}

</div>

</div>

<div >

<div >

<strong>Role:</strong>

{!! Form::select('roles[]', $roles,$userRole, array('class' => 'form-control','multiple')) !!}

</div>

</div>

<div >

<button type="submit" >Submit</button>

</div>

</div>

{!! Form::close() !!}


<p ><small>Tutorial by ItSolutionStuff.com</small></p>

@endsection

resources/views/users/show.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<div >

<h2> Show User</h2>

</div>

<div >

<a href="{{ route('users.index') }}"> Back</a>

</div>

</div>

</div>


<div >

<div >

<div >

<strong>Name:</strong>

{{ $user->name }}

</div>

</div>

<div >

<div >

<strong>Email:</strong>

{{ $user->email }}

</div>

</div>

<div >

<div >

<strong>Roles:</strong>

@if(!empty($user->getRoleNames()))

@foreach($user->getRoleNames() as $v)

<label >{{ $v }}</label>

@endforeach

@endif

</div>

</div>

</div>

@endsection

resources/views/roles/index.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<div >

<h2>Role Management</h2>

</div>

<div >

@can('role-create')

<a href="{{ route('roles.create') }}"> Create New Role</a>

@endcan

</div>

</div>

</div>


@if ($message = Session::get('success'))

<div >

<p>{{ $message }}</p>

</div>

@endif


<table >

<tr>

<th>No</th>

<th>Name</th>

<th width="280px">Action</th>

</tr>

@foreach ($roles as $key => $role)

<tr>

<td>{{ ++$i }}</td>

<td>{{ $role->name }}</td>

<td>

<a href="{{ route('roles.show',$role->id) }}">Show</a>

@can('role-edit')

<a href="{{ route('roles.edit',$role->id) }}">Edit</a>

@endcan

@can('role-delete')

{!! Form::open(['method' => 'DELETE','route' => ['roles.destroy', $role->id],'style'=>'display:inline']) !!}

{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}

{!! Form::close() !!}

@endcan

</td>

</tr>

@endforeach

</table>


{!! $roles->render() !!}


<p ><small>Tutorial by ItSolutionStuff.com</small></p>

@endsection

resources/views/roles/create.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<div >

<h2>Create New Role</h2>

</div>

<div >

<a href="{{ route('roles.index') }}"> Back</a>

</div>

</div>

</div>


@if (count($errors) > 0)

<div >

<strong>Whoops!</strong> There were some problems with your input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


{!! Form::open(array('route' => 'roles.store','method'=>'POST')) !!}

<div >

<div >

<div >

<strong>Name:</strong>

{!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}

</div>

</div>

<div >

<div >

<strong>Permission:</strong>

<br/>

@foreach($permission as $value)

<label>{{ Form::checkbox('permission[]', $value->id, false, array('class' => 'name')) }}

{{ $value->name }}</label>

<br/>

@endforeach

</div>

</div>

<div >

<button type="submit" >Submit</button>

</div>

</div>

{!! Form::close() !!}


<p ><small>Tutorial by ItSolutionStuff.com</small></p>

@endsection

resources/views/roles/edit.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<div >

<h2>Edit Role</h2>

</div>

<div >

<a href="{{ route('roles.index') }}"> Back</a>

</div>

</div>

</div>


@if (count($errors) > 0)

<div >

<strong>Whoops!</strong> There were some problems with your input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


{!! Form::model($role, ['method' => 'PATCH','route' => ['roles.update', $role->id]]) !!}

<div >

<div >

<div >

<strong>Name:</strong>

{!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}

</div>

</div>

<div >

<div >

<strong>Permission:</strong>

<br/>

@foreach($permission as $value)

<label>{{ Form::checkbox('permission[]', $value->id, in_array($value->id, $rolePermissions) ? true : false, array('class' => 'name')) }}

{{ $value->name }}</label>

<br/>

@endforeach

</div>

</div>

<div >

<button type="submit" >Submit</button>

</div>

</div>

{!! Form::close() !!}


@endsection

<p ><small>Tutorial by ItSolutionStuff.com</small></p>

resources/views/roles/show.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<div >

<h2> Show Role</h2>

</div>

<div >

<a href="{{ route('roles.index') }}"> Back</a>

</div>

</div>

</div>


<div >

<div >

<div >

<strong>Name:</strong>

{{ $role->name }}

</div>

</div>

<div >

<div >

<strong>Permissions:</strong>

@if(!empty($rolePermissions))

@foreach($rolePermissions as $v)

<label >{{ $v->name }},</label>

@endforeach

@endif

</div>

</div>

</div>

@endsection

resources/views/products/index.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<div >

<h2>Products</h2>

</div>

<div >

@can('product-create')

<a href="{{ route('products.create') }}"> Create New Product</a>

@endcan

</div>

</div>

</div>


@if ($message = Session::get('success'))

<div >

<p>{{ $message }}</p>

</div>

@endif


<table >

<tr>

<th>No</th>

<th>Name</th>

<th>Details</th>

<th width="280px">Action</th>

</tr>

@foreach ($products as $product)

<tr>

<td>{{ ++$i }}</td>

<td>{{ $product->name }}</td>

<td>{{ $product->detail }}</td>

<td>

<form action="{{ route('products.destroy',$product->id) }}" method="POST">

<a href="{{ route('products.show',$product->id) }}">Show</a>

@can('product-edit')

<a href="{{ route('products.edit',$product->id) }}">Edit</a>

@endcan


@csrf

@method('DELETE')

@can('product-delete')

<button type="submit" >Delete</button>

@endcan

</form>

</td>

</tr>

@endforeach

</table>


{!! $products->links() !!}


<p ><small>Tutorial by ItSolutionStuff.com</small></p>

@endsection

resources/views/products/create.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<div >

<h2>Add New Product</h2>

</div>

<div >

<a href="{{ route('products.index') }}"> Back</a>

</div>

</div>

</div>


@if ($errors->any())

<div >

<strong>Whoops!</strong> There were some problems with your input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


<form action="{{ route('products.store') }}" method="POST">

@csrf


<div >

<div >

<div >

<strong>Name:</strong>

<input type="text" name="name" placeholder="Name">

</div>

</div>

<div >

<div >

<strong>Detail:</strong>

<textarea style="height:150px" name="detail" placeholder="Detail"></textarea>

</div>

</div>

<div >

<button type="submit" >Submit</button>

</div>

</div>


</form>


<p ><small>Tutorial by ItSolutionStuff.com</small></p>

@endsection

resources/views/products/edit.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<div >

<h2>Edit Product</h2>

</div>

<div >

<a href="{{ route('products.index') }}"> Back</a>

</div>

</div>

</div>


@if ($errors->any())

<div >

<strong>Whoops!</strong> There were some problems with your input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


<form action="{{ route('products.update',$product->id) }}" method="POST">

@csrf

@method('PUT')


<div >

<div >

<div >

<strong>Name:</strong>

<input type="text" name="name" value="{{ $product->name }}" placeholder="Name">

</div>

</div>

<div >

<div >

<strong>Detail:</strong>

<textarea style="height:150px" name="detail" placeholder="Detail">{{ $product->detail }}</textarea>

</div>

</div>

<div >

<button type="submit" >Submit</button>

</div>

</div>


</form>


<p ><small>Tutorial by ItSolutionStuff.com</small></p>

@endsection

resources/views/products/show.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<div >

<h2> Show Product</h2>

</div>

<div >

<a href="{{ route('products.index') }}"> Back</a>

</div>

</div>

</div>


<div >

<div >

<div >

<strong>Name:</strong>

{{ $product->name }}

</div>

</div>

<div >

<div >

<strong>Details:</strong>

{{ $product->detail }}

</div>

</div>

</div>

@endsection

<p ><small>Tutorial by ItSolutionStuff.com</small></p>

Step 10: Handle Exertion Error

Now, in this step we will handle exertion. if you don’t have a permission and try to access that page using browser url then you can give message as like bellow:

add/Exceptions/Handler.php

......

public function render($request, Exception $exception)

{

if ($exception instanceof SpatiePermissionExceptionsUnauthorizedException) {

return response()->json(['User have not permission for this page access.']);

}

return parent::render($request, $exception);

}

....

Step 11: Create Seeder For Permissions and AdminUser

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:

1.role-list

2.role-create

3.role-edit

4.role-delete

5.product-list

6.product-create

7.product-edit

8.product-delete

So, first create seeder using bellow command:

php artisan make:seeder PermissionTableSeeder

And put bellow code in PermissionTableSeeder seeder this way:

database/seeds/PermissionTableSeeder.php

<?php


use IlluminateDatabaseSeeder;

use SpatiePermissionModelsPermission;


class PermissionTableSeeder extends Seeder

{

/**

* Run the database seeds.

*

* @return void

*/

public function run()

{

$permissions = [

'role-list',

'role-create',

'role-edit',

'role-delete',

'product-list',

'product-create',

'product-edit',

'product-delete'

];


foreach ($permissions as $permission) {

Permission::create(['name' => $permission]);

}

}

}

After this we have to run bellow command for run PermissionTableSeeder seeder:

php artisan db:seed --class=PermissionTableSeeder

Now let’s create new seeder for creating admin user.

php artisan make:seeder CreateAdminUserSeeder

database/seeds/PermissionTableSeeder.php

<?php

use IlluminateDatabaseSeeder;

use AppUser;

use SpatiePermissionModelsRole;

use SpatiePermissionModelsPermission;

class CreateAdminUserSeeder extends Seeder

{

/**

* Run the database seeds.

*

* @return void

*/

public function run()

{

$user = User::create([

'name' => 'Hardik Savani',

'email' => 'admin@gmail.com',

'password' => bcrypt('123456')

]);

$role = Role::create(['name' => 'Admin']);

$permissions = Permission::pluck('id','id')->all();

$role->syncPermissions($permissions);

$user->assignRole([$role->id]);

}

}

php artisan db:seed --class=CreateAdminUserSeeder

Now we are ready to to run full example of ACL. so let’s run our example so run bellow command for quick run:

php artisan serve

Access By

http://localhost:8000/

Now you can login with following credential:

Also see:Stripe Payment Gateway Integration in Laravel 5.8

Email: admin@gmail.com

Password: 123456

You can see bellow screenshots:

You can download code from git: Download Code from Github

I hope it can help you….

Hope this code and post will helped you for implement Laravel 5.8 User Roles and Permissions Tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

77  +    =  79

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US