Laravel 5.2 – User ACL Roles and Permissions with Middleware using entrust from Scratch Tutorial

Laravel 5.2 – User ACL Roles and Permissions with Middleware using entrust from Scratch Tutorial

In this post we will give you information about Laravel 5.2 – User ACL Roles and Permissions with Middleware using entrust from Scratch Tutorial. Hear we will give you detail about Laravel 5.2 – User ACL Roles and Permissions with Middleware using entrust from Scratch TutorialAnd how to use it also give you demo for it if it is necessary.

Laravel 5 provides authentication to us but that it simple to get user register, login, logout, and reset password and run quickly and easily. Laravel 5 give you simple authentication and it’s fast and it’s consider to all developer requirement.

But if you are work on big ERP or Project then you need to control access to certain sections of the website. I mean you require to role permissions based access control database design that way you can specify level of user.

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.

So if you also want to build ACL(Access Control List) based on Roles and Permissions with Middleware then you can do it simple by following few step. In this tutorial i give you very simple step to create ACL from scratch using entrust package, it is provides lots of method to check permission and roles, so no worry if you don’t know more laravel.

In this examples i created three modules as listed bellow:

User Management

Role Management

Item CRUD Management

After registration, 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, item-list, item-create, item-edit, item-delete. you can check with assign new user and check that.

After complete you can see as bellow perview:

Preview:

role based access control database design, user role permission laravel, laravel role and permission, laravel 5 user roles and permissions, laravel 5.2 permissions, laravel 5 roles and permissions tutorial, laravel 5 entrust middleware, zizaco entrust laravel 5, entrust laravel 5 tutorial, laravel roles tutorial, laravel user roles tutorial, laravel acl roles and permissions

Step 1: Laravel Installation

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

After clone laravel application, we also require to install laravelcollective/html for Form class, you can install from here : HTML/FORM not found in Laravel 5?.

Step 2: Package Installation

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

composer require zizaco/entrust:5.2.x-dev

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

'providers' => [

....

'ZizacoEntrustEntrustServiceProvider::class',

],

'aliases' => [

....

'Entrust' => ZizacoEntrustEntrustFacade::class,

],

Config

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

php artisan vendor:publish

Step 3: Create Table using Migration

In this step we have to create five tables as listed bellow :

1.users

2.items

3.roles

4.role_user

5.permission_role

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

php artisan make:migration create_items_table

php artisan entrust:migration

User table Migration

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


class CreateUsersTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

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

$table->increments('id');

$table->string('name');

$table->string('email')->unique();

$table->string('password');

$table->rememberToken();

$table->timestamps();

});

}


/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::drop('users');

}

}

Item table Migration

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


class CreateItemsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

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

$table->increments('id');

$table->string('title');

$table->text('description');

$table->timestamps();

});

}


/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::drop("items");

}

}

Entrust tables Migration

use IlluminateDatabaseMigrationsMigration;

use IlluminateDatabaseSchemaBlueprint;


class EntrustSetupTables extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

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

$table->increments('id');

$table->string('name')->unique();

$table->string('display_name')->nullable();

$table->string('description')->nullable();

$table->timestamps();

});


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

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

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


$table->foreign('user_id')->references('id')->on('users')

->onUpdate('cascade')->onDelete('cascade');

$table->foreign('role_id')->references('id')->on('roles')

->onUpdate('cascade')->onDelete('cascade');


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

});


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

$table->increments('id');

$table->string('name')->unique();

$table->string('display_name')->nullable();

$table->string('description')->nullable();

$table->timestamps();

});


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

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

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


$table->foreign('permission_id')->references('id')->on('permissions')

->onUpdate('cascade')->onDelete('cascade');

$table->foreign('role_id')->references('id')->on('roles')

->onUpdate('cascade')->onDelete('cascade');


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

});

}


/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::drop('permission_role');

Schema::drop('permissions');

Schema::drop('role_user');

Schema::drop('roles');

}

}


Step 4: Create Model

In this step we have to create model for User, Item, Role and Permission 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

namespace App;


use IlluminateFoundationAuthUser as Authenticatable;

use ZizacoEntrustTraitsEntrustUserTrait;


class User extends Authenticatable

{

use EntrustUserTrait;

/**

* 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',

];

}

app/Item.php

namespace App;


use IlluminateDatabaseEloquentModel;


class Item extends Model

{


public $fillable = ['title','description'];


}

app/Role.php

use ZizacoEntrustEntrustRole;


class Role extends EntrustRole

{

}

app/Permission.php

use ZizacoEntrustEntrustPermission;


class Permission extends EntrustPermission

{

}

Step 5: Add Middleware

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

role

permission

ability

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

app/Http/Kernel.php

....

protected $routeMiddleware = [

....

'role' => ZizacoEntrustMiddlewareEntrustRole::class,

'permission' => ZizacoEntrustMiddlewareEntrustPermission::class,

'ability' => ZizacoEntrustMiddlewareEntrustAbility::class,

]

....

Step 6: Create Seeder For Permission

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.item-list

6.item-create

7.item-edit

8.item-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

use IlluminateDatabaseSeeder;

use AppPermission;


class PermissionTableSeeder extends Seeder

{

/**

* Run the database seeds.

*

* @return void

*/

public function run()

{

$permission = [

[

'name' => 'role-list',

'display_name' => 'Display Role Listing',

'description' => 'See only Listing Of Role'

],

[

'name' => 'role-create',

'display_name' => 'Create Role',

'description' => 'Create New Role'

],

[

'name' => 'role-edit',

'display_name' => 'Edit Role',

'description' => 'Edit Role'

],

[

'name' => 'role-delete',

'display_name' => 'Delete Role',

'description' => 'Delete Role'

],

[

'name' => 'item-list',

'display_name' => 'Display Item Listing',

'description' => 'See only Listing Of Item'

],

[

'name' => 'item-create',

'display_name' => 'Create Item',

'description' => 'Create New Item'

],

[

'name' => 'item-edit',

'display_name' => 'Edit Item',

'description' => 'Edit Item'

],

[

'name' => 'item-delete',

'display_name' => 'Delete Item',

'description' => 'Delete Item'

]

];


foreach ($permission as $key => $value) {

Permission::create($value);

}

}

}

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

php artisan db:seed --class=PermissionTableSeeder

Step 7: Create Authentication

In this step we require to create authentication of Laravel 5.2, 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 8: Add Route

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

app/Http/routes.php

Route::get('/', function () {

return view('welcome');

});


Route::auth();


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


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


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


Route::get('roles',['as'=>'roles.index','uses'=>'RoleController@index','middleware' => ['permission:role-list|role-create|role-edit|role-delete']]);

Route::get('roles/create',['as'=>'roles.create','uses'=>'RoleController@create','middleware' => ['permission:role-create']]);

Route::post('roles/create',['as'=>'roles.store','uses'=>'RoleController@store','middleware' => ['permission:role-create']]);

Route::get('roles/{id}',['as'=>'roles.show','uses'=>'RoleController@show']);

Route::get('roles/{id}/edit',['as'=>'roles.edit','uses'=>'RoleController@edit','middleware' => ['permission:role-edit']]);

Route::patch('roles/{id}',['as'=>'roles.update','uses'=>'RoleController@update','middleware' => ['permission:role-edit']]);

Route::delete('roles/{id}',['as'=>'roles.destroy','uses'=>'RoleController@destroy','middleware' => ['permission:role-delete']]);


Route::get('itemCRUD2',['as'=>'itemCRUD2.index','uses'=>'ItemCRUD2Controller@index','middleware' => ['permission:item-list|item-create|item-edit|item-delete']]);

Route::get('itemCRUD2/create',['as'=>'itemCRUD2.create','uses'=>'ItemCRUD2Controller@create','middleware' => ['permission:item-create']]);

Route::post('itemCRUD2/create',['as'=>'itemCRUD2.store','uses'=>'ItemCRUD2Controller@store','middleware' => ['permission:item-create']]);

Route::get('itemCRUD2/{id}',['as'=>'itemCRUD2.show','uses'=>'ItemCRUD2Controller@show']);

Route::get('itemCRUD2/{id}/edit',['as'=>'itemCRUD2.edit','uses'=>'ItemCRUD2Controller@edit','middleware' => ['permission:item-edit']]);

Route::patch('itemCRUD2/{id}',['as'=>'itemCRUD2.update','uses'=>'ItemCRUD2Controller@update','middleware' => ['permission:item-edit']]);

Route::delete('itemCRUD2/{id}',['as'=>'itemCRUD2.destroy','uses'=>'ItemCRUD2Controller@destroy','middleware' => ['permission:item-delete']]);

});

Step 9: Add Controller

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

app/Http/Controllers/UserController.php

namespace AppHttpControllers;


use IlluminateHttpRequest;

use AppHttpControllersController;

use AppUser;

use AppRole;

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::lists('display_name','id');

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

foreach ($request->input('roles') as $key => $value) {

$user->attachRole($value);

}


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::lists('display_name','id');

$userRole = $user->roles->lists('id','id')->toArray();


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('role_user')->where('user_id',$id)->delete();


foreach ($request->input('roles') as $key => $value) {

$user->attachRole($value);

}


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/ItemCRUD2Controller.php

namespace AppHttpControllers;


use IlluminateHttpRequest;

use AppHttpControllersController;

use AppItem;


class ItemCRUD2Controller extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function index(Request $request)

{

$items = Item::orderBy('id','DESC')->paginate(5);

return view('ItemCRUD2.index',compact('items'))

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

}


/**

* Show the form for creating a new resource.

*

* @return IlluminateHttpResponse

*/

public function create()

{

return view('ItemCRUD2.create');

}


/**

* Store a newly created resource in storage.

*

* @param IlluminateHttpRequest $request

* @return IlluminateHttpResponse

*/

public function store(Request $request)

{

$this->validate($request, [

'title' => 'required',

'description' => 'required',

]);


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


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

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

}


/**

* Display the specified resource.

*

* @param int $id

* @return IlluminateHttpResponse

*/

public function show($id)

{

$item = Item::find($id);

return view('ItemCRUD2.show',compact('item'));

}


/**

* Show the form for editing the specified resource.

*

* @param int $id

* @return IlluminateHttpResponse

*/

public function edit($id)

{

$item = Item::find($id);

return view('ItemCRUD2.edit',compact('item'));

}


/**

* Update the specified resource in storage.

*

* @param IlluminateHttpRequest $request

* @param int $id

* @return IlluminateHttpResponse

*/

public function update(Request $request, $id)

{

$this->validate($request, [

'title' => 'required',

'description' => 'required',

]);


Item::find($id)->update($request->all());


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

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

}


/**

* Remove the specified resource from storage.

*

* @param int $id

* @return IlluminateHttpResponse

*/

public function destroy($id)

{

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

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

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

}

}

app/Http/Controllers/RoleController.php

namespace AppHttpControllers;


use IlluminateHttpRequest;

use AppHttpControllersController;

use AppRole;

use AppPermission;

use DB;


class RoleController extends Controller

{

/**

* 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',

'display_name' => 'required',

'description' => 'required',

'permission' => 'required',

]);


$role = new Role();

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

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

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

$role->save();


foreach ($request->input('permission') as $key => $value) {

$role->attachPermission($value);

}


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("permission_role","permission_role.permission_id","=","permissions.id")

->where("permission_role.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("permission_role")->where("permission_role.role_id",$id)

->lists('permission_role.permission_id','permission_role.permission_id');


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

'display_name' => 'required',

'description' => 'required',

'permission' => 'required',

]);


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

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

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

$role->save();


DB::table("permission_role")->where("permission_role.role_id",$id)

->delete();


foreach ($request->input('permission') as $key => $value) {

$role->attachPermission($value);

}


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 10: Add View Files

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

resources/views/layouts/app.blade.php

<html lang="en">

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Laravel</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css" integrity="sha384-XdYbMnZ/QjLh6iI4ogqCTaIjrFk87ip+ekIjefZch0Y+PvJ8CDYtEs1ipDmPorQ+" crossorigin="anonymous">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:100,300,400,700">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" crossorigin="anonymous">

</head>

<body id="app-layout">

<nav >

<div >

<div >

<!-- Collapsed Hamburger -->

<button type="button" data-toggle="collapse" data-target="#app-navbar-collapse">

<span >Toggle Navigation</span>

<span ></span>

<span ></span>

<span ></span>

</button>

<!-- Branding Image -->

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

Laravel

</a>

</div>

<div id="app-navbar-collapse">

<!-- Left Side Of Navbar -->

<ul >

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

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

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

<li><a href="{{ route('itemCRUD2.index') }}">Items</a></li>

</ul>

<!-- Right Side Of Navbar -->

<ul >

<!-- Authentication Links -->

@if (Auth::guest())

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

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

@else

<li >

<a href="#" data-toggle="dropdown" role="button" aria-expanded="false">

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

</a>

<ul role="menu">

<li><a href="{{ url('/logout') }}"><i ></i>Logout</a></li>

</ul>

</li>

@endif

</ul>

</div>

</div>

</nav>

<div >

@yield('content')

</div>

<!-- JavaScripts -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js" integrity="sha384-I6F5OKECLVtK/BL+8iSLDEHowSAfUo76ZL9+kGAgTRdiByINKJaqTPH/QVNS1VDb" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

</body>

</html>

resources/views/errors/403.blade.php

@extends('layouts.app')

@section('content')

<h1>You don't have permission.</h1>

@endsection

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

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

<label >{{ $v->display_name }}</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() !!}

@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() !!}

@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() !!}

@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->roles))

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

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

@endforeach

@endif

</div>

</div>

</div>

@endsection

resources/views/ItemCRUD2/index.blade.php

@extends('layouts.app')

@section('content')

<div >

<div >

<div >

<h2>Items CRUD</h2>

</div>

<div >

@permission('item-create')

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

@endpermission

</div>

</div>

</div>

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

<div >

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

</div>

@endif

<table >

<tr>

<th>No</th>

<th>Title</th>

<th>Description</th>

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

</tr>

@foreach ($items as $key => $item)

<tr>

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

<td>{{ $item->title }}</td>

<td>{{ $item->description }}</td>

<td>

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

@permission('item-edit')

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

@endpermission

@permission('item-delete')

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

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

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

@endpermission

</td>

</tr>

@endforeach

</table>

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

@endsection

resources/views/ItemCRUD2/create.blade.php

@extends('layouts.app')

@section('content')

<div >

<div >

<div >

<h2>Create New Item</h2>

</div>

<div >

<a href="{{ route('itemCRUD2.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' => 'itemCRUD2.store','method'=>'POST')) !!}

<div >

<div >

<div >

<strong>Title:</strong>

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

</div>

</div>

<div >

<div >

<strong>Description:</strong>

{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px')) !!}

</div>

</div>

<div >

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

</div>

</div>

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

@endsection

resources/views/ItemCRUD2/edit.blade.php

@extends('layouts.app')

@section('content')

<div >

<div >

<div >

<h2>Edit New Item</h2>

</div>

<div >

<a href="{{ route('itemCRUD2.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($item, ['method' => 'PATCH','route' => ['itemCRUD2.update', $item->id]]) !!}

<div >

<div >

<div >

<strong>Title:</strong>

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

</div>

</div>

<div >

<div >

<strong>Description:</strong>

{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px')) !!}

</div>

</div>

<div >

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

</div>

</div>

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

@endsection

resources/views/ItemCRUD2/show.blade.php

@extends('layouts.app')

@section('content')

<div >

<div >

<div >

<h2> Show Item</h2>

</div>

<div >

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

</div>

</div>

</div>

<div >

<div >

<div >

<strong>Title:</strong>

{{ $item->title }}

</div>

</div>

<div >

<div >

<strong>Description:</strong>

{{ $item->description }}

</div>

</div>

</div>

@endsection

resources/views/roles/index.blade.php

@extends('layouts.app')

@section('content')

<div >

<div >

<div >

<h2>Role Management</h2>

</div>

<div >

@permission('role-create')

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

@endpermission

</div>

</div>

</div>

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

<div >

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

</div>

@endif

<table >

<tr>

<th>No</th>

<th>Name</th>

<th>Description</th>

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

</tr>

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

<tr>

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

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

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

<td>

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

@permission('role-edit')

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

@endpermission

@permission('role-delete')

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

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

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

@endpermission

</td>

</tr>

@endforeach

</table>

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

@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>Display Name:</strong>

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

</div>

</div>

<div >

<div >

<strong>Description:</strong>

{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px')) !!}

</div>

</div>

<div >

<div >

<strong>Permission:</strong>

<br/>

@foreach($permission as $value)

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

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

<br/>

@endforeach

</div>

</div>

<div >

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

</div>

</div>

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

@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('display_name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}

</div>

</div>

<div >

<div >

<strong>Description:</strong>

{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px')) !!}

</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->display_name }}</label>

<br/>

@endforeach

</div>

</div>

<div >

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

</div>

</div>

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

@endsection

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

</div>

</div>

<div >

<div >

<strong>Description:</strong>

{{ $role->description }}

</div>

</div>

<div >

<div >

<strong>Permissions:</strong>

@if(!empty($rolePermissions))

@foreach($rolePermissions as $v)

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

@endforeach

@endif

</div>

</div>

</div>

@endsection

Ok, now you can run your application and check if you find error like

This cache store does not support tagging

Then you can solve from here : Click Here

And if you want to get more information about entrust package then click here : Entrust.

Hope this code and post will helped you for implement Laravel 5.2 – User ACL Roles and Permissions with Middleware using entrust from Scratch 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  +    =  81

We accept paid advance and paid guest Posting on our Site : Contact US