Sunteți pe pagina 1din 34

Popular Post

(http://itsolutionstu .com/popular-post)

Laravel Popular Post


(http://itsolutionstu .com/laravel-popular)

Featured Post
(http://itsolutionstu .com/featured-post)

Laravel 5.2 - User ACL Roles and Permissions with Middleware using
entrust from Scratch Tutorial
By Hardik Savani (https://www.linkedin.com/in/savanihd) | July 4, 2016 | | 456314 Viewer | Category : Laravel

Share this post:

Like +1 Tweet Share Pin it Share

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 di erent 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
A er registration, you don't have any roles, so you can edit your details and assign admin role to you from User Management. A er
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.
A er 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

A er 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? (http://itsolutionstu .com/post/html-form-not-found-in-laravel-5example.html).

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' => [
....
'Zizaco\Entrust\EntrustServiceProvider::class',
],
'aliases' => [
....
'Entrust' => Zizaco\Entrust\EntrustFacade::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 Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

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 Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

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 Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

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 Illuminate\Foundation\Auth\User as Authenticatable;


use Zizaco\Entrust\Traits\EntrustUserTrait;

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 Illuminate\Database\Eloquent\Model;

class Item extends Model


{

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

app/Role.php

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole


{
}

app/Permission.php

use Zizaco\Entrust\EntrustPermission;

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' => \Zizaco\Entrust\Middleware\EntrustRole::class,
'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::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 Illuminate\Database\Seeder;
use App\Permission;

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

A er 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|ro


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


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

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 App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Role;
use DB;
use Hash;

class UserController extends Controller


{

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
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 \Illuminate\Http\Response
*/
public function create()
{
$roles = Role::lists('display_name','id');
return view('users.create',compact('roles'));
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
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 \Illuminate\Http\Response
*/
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 \Illuminate\Http\Response
*/
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 \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
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 \Illuminate\Http\Response
*/
public function destroy($id)
{
User::find($id)->delete();
return redirect()->route('users.index')
->with('success','User deleted successfully');
}
}

app/Http/Controllers/ItemCRUD2Controller.php


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Item;

class ItemCRUD2Controller extends Controller


{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
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 \Illuminate\Http\Response
*/
public function create()
{
return view('ItemCRUD2.create');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
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 \Illuminate\Http\Response
*/
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 \Illuminate\Http\Response
*/
public function edit($id)
{
$item = Item::find($id);
return view('ItemCRUD2.edit',compact('item'));
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
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 \Illuminate\Http\Response
*/
public function destroy($id)
{
Item::find($id)->delete();
return redirect()->route('itemCRUD2.index')
->with('success','Item deleted successfully');
}
}

app/Http/Controllers/RoleController.php


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Role;
use App\Permission;
use DB;

class RoleController extends Controller


{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
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 \Illuminate\Http\Response
*/
public function create()
{
$permission = Permission::get();
return view('roles.create',compact('permission'));
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
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 \Illuminate\Http\Response
*/
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 \Illuminate\Http\Response
*/
public function edit($id)

{
$role = Role::find($id);
$permission = Permission::get();
$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 \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
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 \Illuminate\Http\Response
*/
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
<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 class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<!-- Collapsed Hamburger -->
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- Branding Image -->
<a class="navbar-brand" href="{{ url('/') }}">
Laravel
</a>
</div>
<div class="collapse navbar-collapse" id="app-navbar-collapse">
<!-- Left Side Of Navbar -->
<ul class="nav navbar-nav">
<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 class="nav navbar-nav navbar-right">
<!-- Authentication Links -->
@if (Auth::guest())
<li><a href="{{ url('/login') }}">Login</a></li>
<li><a href="{{ url('/register') }}">Register</a></li>
@else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li><a href="{{ url('/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li>
</ul>
</li>
@endif
</ul>
</div>
</div>
</nav>
<div class="container">
@yield('content')
</div>
<!-- JavaScripts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js" integrity="sha384-I6F5OKECLVtK/BL+8iSLDEHowSAfUo76ZL9+kGAg
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qr
</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 class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Users Management</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('users.create') }}"> Create New User</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<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 class="label label-success">{{ $v->display_name }}</label>
@endforeach
@endif
</td>
<td>
<a class="btn btn-info" href="{{ route('users.show',$user->id) }}">Show</a>
<a class="btn btn-primary" 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 class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Create New User</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('users.index') }}"> Back</a>
</div>
</div>
</div>
@if (count($errors) > 0)
<div class="alert alert-danger">
<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 class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Email:</strong>
{!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Password:</strong>
{!! Form::password('password', array('placeholder' => 'Password','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Confirm Password:</strong>
{!! Form::password('confirm-password', array('placeholder' => 'Confirm Password','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Role:</strong>
{!! Form::select('roles[]', $roles,[], array('class' => 'form-control','multiple')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
@endsection

resources/views/users/edit.blade.php


@extends('layouts.app')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit New User</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('users.index') }}"> Back</a>
</div>
</div>
</div>
@if (count($errors) > 0)
<div class="alert alert-danger">
<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 class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Email:</strong>
{!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Password:</strong>
{!! Form::password('password', array('placeholder' => 'Password','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Confirm Password:</strong>
{!! Form::password('confirm-password', array('placeholder' => 'Confirm Password','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Role:</strong>
{!! Form::select('roles[]', $roles,$userRole, array('class' => 'form-control','multiple')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
@endsection

resources/views/users/show.blade.php


@extends('layouts.app')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show User</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('users.index') }}"> Back</a>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{{ $user->name }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Email:</strong>
{{ $user->email }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Roles:</strong>
@if(!empty($user->roles))
@foreach($user->roles as $v)
<label class="label label-success">{{ $v->display_name }}</label>
@endforeach
@endif
</div>
</div>
</div>
@endsection

resources/views/ItemCRUD2/index.blade.php


@extends('layouts.app')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Items CRUD</h2>
</div>
<div class="pull-right">
@permission('item-create')
<a class="btn btn-success" href="{{ route('itemCRUD2.create') }}"> Create New Item</a>
@endpermission
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<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 class="btn btn-info" href="{{ route('itemCRUD2.show',$item->id) }}">Show</a>
@permission('item-edit')
<a class="btn btn-primary" 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 class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Create New Item</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('itemCRUD2.index') }}"> Back</a>
</div>
</div>
</div>
@if (count($errors) > 0)
<div class="alert alert-danger">
<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 class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Title:</strong>
{!! Form::text('title', null, array('placeholder' => 'Title','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
@endsection

resources/views/ItemCRUD2/edit.blade.php


@extends('layouts.app')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit New Item</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('itemCRUD2.index') }}"> Back</a>
</div>
</div>
</div>
@if (count($errors) > 0)
<div class="alert alert-danger">
<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 class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Title:</strong>
{!! Form::text('title', null, array('placeholder' => 'Title','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
@endsection

resources/views/ItemCRUD2/show.blade.php

@extends('layouts.app')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show Item</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('itemCRUD2.index') }}"> Back</a>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Title:</strong>
{{ $item->title }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
{{ $item->description }}
</div>
</div>
</div>
@endsection

resources/views/roles/index.blade.php


@extends('layouts.app')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Role Management</h2>
</div>
<div class="pull-right">
@permission('role-create')
<a class="btn btn-success" href="{{ route('roles.create') }}"> Create New Role</a>
@endpermission
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<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 class="btn btn-info" href="{{ route('roles.show',$role->id) }}">Show</a>
@permission('role-edit')
<a class="btn btn-primary" 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 class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Create New Role</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('roles.index') }}"> Back</a>
</div>
</div>
</div>
@if (count($errors) > 0)
<div class="alert alert-danger">
<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 class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Display Name:</strong>
{!! Form::text('display_name', null, array('placeholder' => 'Display Name','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<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 class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
@endsection

resources/views/roles/edit.blade.php


@extends('layouts.app')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Role</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('roles.index') }}"> Back</a>
</div>
</div>
</div>
@if (count($errors) > 0)
<div class="alert alert-danger">
<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 class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{!! Form::text('display_name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Permission:</strong>
<br/>
@foreach($permission as $value)
<label>{{ Form::checkbox('permission[]', $value->id, in_array($value->id, $rolePermissions) ? true : false, array('cl
{{ $value->display_name }}</label>
<br/>
@endforeach
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
@endsection

resources/views/roles/show.blade.php


@extends('layouts.app')

@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show Role</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('roles.index') }}"> Back</a>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{{ $role->display_name }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
{{ $role->description }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Permissions:</strong>
@if(!empty($rolePermissions))
@foreach($rolePermissions as $v)
<label class="label label-success">{{ $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 (http://itsolutionstu .com/post/how-to-solve-this-cache-store-does-not-support-
taggingexample.html)
And if you want to get more information about entrust package then click here : Entrust (https://github.com/Zizaco/entrust).

Show Demo
(http://demo.itsolutionstu .com/acl/login)

Tags : ACL (http://itsolutionstu .com/tag/acl.html) Source Code (http://itsolutionstu .com/tag/code.html) Demo (http://itsolutionstu .com/tag/demo.html)

Example (http://itsolutionstu .com/tag/example.html) Laravel (http://itsolutionstu .com/tag/laravel.html) Laravel 5 (http://itsolutionstu .com/tag/laravel-5.html)

Laravel 5.2 (http://itsolutionstu .com/tag/laravel-52.html) Permission (http://itsolutionstu .com/tag/permission.html)


GFI LanGuard for Laravel 5.3 - Form Input Equinix SG3 Data Laravel 5 - create quick
Business - Complete... Validation rules... Center backend admin panel...
Ad gfi.com itsolutionstuff.com Ad Equinix itsolutionstuff.com

Management of Data How to create and use Laravel 5 - Simple user CRUD (Create Read
Middleware in Laravel... access control using... Update Delete)...
Ad METTLER TOLEDO itsolutionstuff.com itsolutionstuff.com itsolutionstuff.com

We are Recommending you:

1. Laravel 5 and Vue JS CRUD with Pagination example and demo from scratch (http://itsolutionstu .com/post/laravel-
5-and-vue-js-crud-with-pagination-example-and-demo-from-scratchexample.html)
2. Laravel 5.2 API using JWT authentication tutorial from scratch example (http://itsolutionstu .com/post/laravel-52-
api-using-jwt-authentication-tutorial-from-scratch-exampleexample.html)
3. Laravel 5 import export to excel and csv using maatwebsite example. (http://itsolutionstu .com/post/laravel-5-
import-export-to-excel-and-csv-using-maatwebsite-exampleexample.html)
4. Laravel 5.2 and AngularJS CRUD with Search and Pagination Example. (http://itsolutionstu .com/post/laravel-52-
and-angularjs-crud-with-search-and-pagination-exampleexample.html)

120 Comments ItSolutionStuff.com


1 Login

Sort by Best
Recommend 20 Share

Join the discussion

Nesrin Dagli a month ago


............
1 Reply Share

Naj 5 months ago


Thank you for this intersting tutorial. But, the only things working are : displaying users, I can't edit them or display items or roles. I followed your
tutorial step by step
1 Reply Share

Ali Abakan > Naj 3 months ago


Edit Roles, Role_Permission and User Role Tables.
Seeder only put Permissions, you should add roles, role_permissions, and user_roles.
Reply Share

Rand No > Naj 3 months ago


You should add this line to your user model:
use EntrustUserTrait;
as following..



Reply Share

Ramia Khan > Naj 4 months ago


Yeah, exactly...Did u got solution for this ?
Reply Share

Najoua > Naj 5 months ago


I have the exact same problem, can anyone help us please
Reply Share

Luciano Santos > Najoua 4 months ago


if u are using laravel 5.3 or higher
in userController, replace
$userRole = $user->roles->lists('id','id')->toArray();
by
$userRole = $user->roles->pluck('id','id')->toArray();

and each place where lists appear, change to pluck


1 Reply Share

meroka collins a year ago


I also hit the following error when trying to click on roles and items menu;
Call to undefined method Illuminate\Database\Query\Builder::cachedPermissions()

i git cloned the project from the git link above, created the migrations and also seeded the permissions as in this tutorial.
What might be the problem?
I have also changes the ".env" file to have CACHE_DRIVER=array
1 Reply Share

Tomas Benetis > meroka collins 10 months ago


Look to model: app/Role.php do you have exactly the same?

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole


{
}
Reply Share

Hardik Savani Mod > Tomas Benetis 10 months ago


Yes, right exactly same.
you can see here on my github : https://github.com/savanihd...
Reply Share

Tomas Benetis > Hardik Savani 10 months ago


Looks everything find. Well if composer dump-autoload and composer update not helping i have no idea.. :(
Reply Share

Hardik Savani Mod > Tomas Benetis 10 months ago


can you tell me what exactly you have error ?
Reply Share

Tomas Benetis > Hardik Savani 10 months ago


Same as yours.
Call to undefined method Illuminate\Database\Query\Builder::cachedPermissions()
Reply Share

Hardik Savani Mod > Tomas Benetis 10 months ago


can you follow this link pls : http://itsolutionstuff.com/...
Reply Share

Tomas Benetis > Hardik Savani 10 months ago


I had same as you. Fixed by extending role model with entrust
Reply Share

Alan 14 days ago


Thank you the tutorial my question is can i use zizaco/entrust in laravel 5.4? if not which other library can i use.
Reply Share

Hardik Savani Mod > Alan 14 days ago


Hardik Savani Mod > Alan 14 days ago
Yes sure you can use with Laravel 5.4
Reply Share

Dheeraj Kukreja a month ago


after running this command
php artisan db:seed --class=PermissionTableSeeder

getting the error mentioned below


PHP Fatal error: Class 'App\Permission' not found in /var/www/html/sms/database/seeds/PermissionTableSeeder.php on line 60

[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'App\Permission' not found

Please help
Reply Share

Prvan Marian > Dheeraj Kukreja 21 days ago


Same don't work.. why?
Reply Share

Dheeraj Kukreja > Prvan Marian 20 days ago


I found the solution....will share soon
Reply Share

Muralidharreddy > Dheeraj Kukreja 22 days ago


add below line to Permission.php file
namespace App;
Reply Share

Nesrin Dagli a month ago

Reply Share

Nesrin Dagli a month ago


logout does not work?
Reply Share

Rahn Haung > Nesrin Dagli 21 days ago


add this line in web.php (if you're using laravel 5.4)

Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
Reply Share

Gustavo Herrera a month ago


good how change this

{!! Form::select('roles[]', $roles,$userRole, array('class' => 'form-control','multiple')) !!} for html?? dont use collective, i use html thanks
Reply Share

Gina Agustina a month ago


why when button roles clicked, its nothing to show? its just show the eror 403, "you don't have permission". can anyone help me please

Reply Share
Sebastin Montenegro 2 months ago
hello Hardik! In a controller I need get the role_id from the logged user, how I can do this?
Reply Share

Ben Abdel Hafidh HaSsen 2 months ago


thank you so much, but what need to change if i am going to assign only one role to each user (without pivot table users_roles).
please help me and thanks.
Reply Share

Abbygail Mendoza 2 months ago


hi. i downloaded this from your github. tried to run it but i had an error:

Reply Share

Hardik Savani Mod > Abbygail Mendoza 2 months ago


Yes, you have to create .env file and copy code from .env.example file
then after run bellow command:
php artisan generate:key
Reply Share

Bijon Krishna Bairagi 2 months ago


Showing this error, please help me. Link is /users/2/edit "Call to undefined method Illuminate\Database\Query\Builder::lists()"

Reply Share

pvrht > Bijon Krishna Bairagi 2 months ago


Laravel 5.3, the lists() method is removed in favour of the pluck() method. You have to op your controllers and change lists() with pluck()
method
Reply Share

Hean Thien 3 months ago


Reply Share

Hean Thien 3 months ago


why class 'Form' not found?
Reply Share

Hardik Savani Mod > Hean Thien 3 months ago


You have to follow this link :
http://itsolutionstuff.com/...
Reply Share

Hu Phm 3 months ago


When I execute SEED, error with 'App\Permission' although I have 'require App\Permission;'... pls
Reply Share

Azizahmad Yazdanpanah 3 months ago


Hi dear friends I used the following codes but I faced with this prob

ErrorException in Gate.php line 321:


Illegal offset type in isset or empty
any one help me
Reply Share

Soft Pdf 4 months ago


There is a problem, I have done everything you instructed on this tutorial but problem when i logged in user i can't set the role or i can't access roles
or items url it show's the 403 Error..Why's that?
should i attach some demo data with that? means like roles assign with the user ??
Reply Share

Sooraj J 4 months ago


How to multiple role individually switching login time
Reply Share

Muzamil Indra 4 months ago


thank you... but, why i got error HttpException like "at EntrustPermission->handle(object(Request), object(Closure)"
i follow your instructions... my user account cant access... even though user having permission 'assign-create'

cant any help me?

my route:
Route::get('/assign',['uses'=>'Management\AssignController@index','middleware' => ['permission:assign-create|assign-delete|assign-edit']]);

blade:

@permission('assign-create')
<li> Assign</li>
@endpermission
Reply Share

developer-2221212111 > Muzamil Indra 2 months ago


i got same error
Reply Share

Hu Hu 4 months ago
how to use dowload?
Reply Share

NonZero 4 months ago


Entrust gives me a error, when I am trying to access a restricted page without login.

But It would be better, redirect me login page in this situation.

How can I remove this problem ?


How can I remove this problem ?

Reply Share

Najoua 5 months ago


Thanks a lot for this intersting tutorial. But, the only things working are : displaying users, and deleting them I can't edit them or display items or
roles. I followed your tutorial step by step
Reply Share

muongi waiganjo > Najoua 5 months ago


have you found a solution am facing the same problem
Reply Share

5 months ago
Thanks for this awesome tutorial. I'm doing it on Laravel 5.2 perfectly.
But I'm wondering how to make a "Searching" for item list ?
Reply Share

Hardik Savani Mod > 5 months ago


For now i don't have simple search for item lists
But you can follow this for full text search : http://itsolutionstuff.com/...
1 Reply Share

> Hardik Savani 5 months ago


Basis on the link you gave me, I finally did it !! :D
But I'm still thinking for some optimization for it ... would you plz give some advise ? Thx ! :D
Here's my code:
------------------(start)

In Route.php add

Route::get('itemCRUD2s', ['as'=>'itemCRUD2.search','uses'=>'ItemCRUD2Controller@search']);


In App.Blade.php add

<form method="GET" action="{{ route('itemCRUD2.search') }}">

<input name="s_text" type="text" placeholder="Please Insert the text" class="form-control" style="width: 130px;">

<button type="submit" class="btn btn-primary">

see more

Reply Share

> Hardik Savani 5 months ago


Thank you so much for sharing this :))
Reply Share

Gkhan Peker 6 months ago


Thanks for the greate tutorial. But I have an error and I can't fix it. I'm using Laravel 5.3

RoleController.php
public function edit($id)
{
$role = Role::find($id);
$permission = Permission::all();
$rolePermissions = DB::table("permission_role")->where("permission_role.role_id",$id)
->pluck('permission_role.permission_id','permission_role.permission_id');

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

role/edit.blade.php
@foreach($permission as $value)
<label>{{ Form::checkbox('permission[]', $value->id, in_array($value->id, $rolePermissions) ? true : false, array('class' => 'name')) }}
{{ $value->display_name }}</label>

@endforeach

Error:

ErrorException in 78e1d929f92067f3abefaf3a9e7cc7f4a8582a16.php line 46:


in_array() expects parameter 2 to be array, object given (View: /home/72749-56479.cloudwaysapps.c...

I think $rolePermissions not seems array. But I can't fix it. Please help me. Thank you.
Reply Share

Load more comments

ALSO ON ITSOLUTIONSTUFF.COM

Laravel - How to generate RSS Feed using roumen/feed package? Laravel 5 - Custom Helper Facade Class Example from scratch
1 comment 2 months ago 1 comment 3 months ago
Robert hi,I'm getting error of "Class 'App\Http\Controllers\App' not Renan Coelho Thank you very much!!!
found" which refers to "$feed = App::make("feed");" what should I do?

Codeigniter - Dynamic dependent dropdown using jquery ajax Laravel 5 - Example of Database Seeder with insert sample data
Example 1 comment 4 months ago
2 comments 20 days ago David Saavedra Why not making use of Model
Israel Parra Why not Codeigniter 4!? Factories(https://laravel.com/docs/5.... for this?You can have a
`UsersFactory`:

Subscribe d Add Disqus to your siteAdd DisqusAdd Privacy

Random Post

1. How to get Query Strings Value in Laravel 5? (http://itsolutionstu .com/post/how-to-get-query-strings-value-in-


laravel-5example.html)

2. How to disable right click on div using context menu jquery? (http://itsolutionstu .com/post/how-to-disable-right-
click-on-div-using-context-menu-jqueryexample.html)

3. How to get Lat and Lon from google map API in Jquery PHP? (http://itsolutionstu .com/post/how-to-get-lat-and-lon-
from-google-map-api-in-jquery-php)

4. How to get days di erence between two dates in laravel? (http://itsolutionstu .com/post/how-to-get-days-di erence-
between-two-dates-in-laravelexample.html)

5. How to remove query string from URL using JQuery (http://itsolutionstu .com/post/how-to-remove-query-string-
from-urlexample.html)

6. PHP - Getting Started PHPUnit test with simple example (http://itsolutionstu .com/post/php-getting-started-phpunit-
test-with-simple-exampleexample.html)

7. Simple Add remove input fields dynamically using jquery with Bootstrap (http://itsolutionstu .com/post/simple-add-
remove-input-fields-dynamically-using-jquery-with-bootstrapexample.html)

8. Laravel 5 - create quick backend admin panel tutorial (http://itsolutionstu .com/post/laravel-5-create-quick-backend-


admin-panel-tutorialexample.html)

Custom Search

Subscribe Your Email address:

Enter Email Subscribe

Connect with us on FB

It Solution Stuff
3,573 likes

Like Page Share

Be the rst of your friends to like this

Popular Posts

Codeigniter 3 - Generate PDF from view using dompdf library with example (http://itsolutionstu .com/post/codeigniter-3-generate-pdf-from-view-using-dompdf-library-with-
exampleexample.html)

How to generate slug using str_slug() helper in Laravel 5? (http://itsolutionstu .com/post/how-to-generate-slug-using-str-slug-helper-in-laravel-5example.html)

How to select concat columns with Laravel Query Builder? (http://itsolutionstu .com/post/how-to-select-concat-columns-with-laravel-query-builderexample.html)
Laravel 5 - Simple user access control using Middleware (http://itsolutionstu .com/post/laravel-5-simple-user-access-control-using-middlewareexample.html)

Laravel 5 - Twitter API using thujohn/twitter composer package tutorial (http://itsolutionstu .com/post/laravel-5-twitter-api-using-thujohn-twitter-composer-package-
tutorialexample.html)

CRUD (Create Read Update Delete) Example in Laravel 5.2 from Scratch (http://itsolutionstu .com/post/crud-create-read-update-delete-example-in-laravel-52-from-
scratchexample.html)

Laravel 5 generate sitemap using roumen/sitemap package Example (http://itsolutionstu .com/post/laravel-5-generate-sitemap-using-roumen-sitemap-package-


exampleexample.html)

How to do remember me function at login option in laravel 5? (http://itsolutionstu .com/post/how-to-do-remember-me-function-at-login-option-in-laravel-5example.html)

Categories

Laravel (http://itsolutionstu .com/category/laravelexample.html)


PHP (http://itsolutionstu .com/category/phpexample.html) jQuery (http://itsolutionstu .com/category/jquery)
Javascript (http://itsolutionstu .com/category/javascriptexample.html)
Bootstrap (http://itsolutionstu .com/category/bootstrap) HTML (http://itsolutionstu .com/category/html)
MySql (http://itsolutionstu .com/category/mysql) AngularJS (http://itsolutionstu .com/category/angularjsexample.html)
Ajax (http://itsolutionstu .com/category/ajaxexample.html)
Installation (http://itsolutionstu .com/category/installationexample.html)
Ubuntu (http://itsolutionstu .com/category/ubuntuexample.html)
Codeigniter (http://itsolutionstu .com/category/codeigniterexample.html)
Git (http://itsolutionstu .com/category/gitexample.html) CSS (http://itsolutionstu .com/category/css)
JSON (http://itsolutionstu .com/category/jsonexample.html)
Node JS (http://itsolutionstu .com/category/node-jsexample.html)
SQL (http://itsolutionstu .com/category/sqlexample.html)
Google API (http://itsolutionstu .com/category/google-apiexample.html)
JQuery UI (http://itsolutionstu .com/category/jquery-uiexample.html)
Google Map (http://itsolutionstu .com/category/google-mapexample.html)
Elasticsearch (http://itsolutionstu .com/category/elasticsearchexample.html)
Server (http://itsolutionstu .com/category/serverexample.html)
Wampserver (http://itsolutionstu .com/category/wampserverexample.html)
Ionic Framework (http://itsolutionstu .com/category/ionic-frameworkexample.html)
Socket.io (http://itsolutionstu .com/category/socketioexample.html)
Sublime (http://itsolutionstu .com/category/sublimeexample.html)

JWT (http://itsolutionstu .com/category/jwtexample.html)

S-ar putea să vă placă și