In this tutorial you will learn about the Laravel 8 User Roles and Permissions Tutorial Example and its application with practical example.
In this Laravel 8 user roles and permissions tutorial example I will show you how to implement user roles and permissions system in laravel 8 spatie/laravel-permission package. In this tutorial you will learn to create user roles and permission in laravel 8 application. We will be using spatie/laravel-permission package to create user roles and permission in laravel 8 application. The Spatie role permission package provide way to implement acl in laravel 8 application. With spatie we can create different user roles, user levels with specific permission. With spatie we can assign user role, assign permission to user and assign permission to roles. In this example we will be using bootstrap auth scaffolding for login, register, logout, forget password, profile and reset password page.
- Laravel 8 User Roles and Permissions Tutorial Example
- Step 1 – Install Laravel 8 App
- Step 2 – Database Configuration
- Step 3 – Install Spatie Composer Packages
- Step 4 – Create Model and Migration
- Step 5 – Register Middleware
- Step 6 – Create Routes
- Step 7 – Create Controllers
- Step 8 – Install Laravel UI
- Step 9 – Install Bootstrap Auth Scaffolding
- Step 10 – Install Npm Packages
- Step 11 – Create Blade Views
- Step 12 – Run Development Server
Laravel 8 User Roles and Permissions Tutorial Example
In this step by step tutorial I will demonstrate you how to create or assign different user roles and user permission using laravel Spatie package. Please follow instructions given below:
Step 1 – Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel Laravel8Auth |
Step 2 – Database Configuration
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=db name DB_USERNAME=db user name DB_PASSWORD=db password |
Step 3 – Install Spatie Composer Packages
In this step, we will install spatie/laravel-permission Package via the composer dependency manager. Use the following command to install spatie/laravel-permission Package.
1 |
composer require spatie/laravel-permission |
1 |
composer require laravelcollective/html |
After Installing Image intervention package, we need to add service provider in config/app.php file as following.
1 |
'providers' => [ .... Spatie\Permission\PermissionServiceProvider::class,], |
Now, run the following command to publish spatie package dependencies:
1 |
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" |
Step 4 – Create Model and Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Product -m |
Now, open migration file name create_products_tables.php and add the follwoing code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateProductsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('products', function (Blueprint $table) { $table->id(); $table->string('name'); $table->text('detail'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('products'); } } |
Now, put the following code into Product.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Product extends Model { use HasFactory; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'detail' ]; } |
Then, put the following code into User.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Spatie\Permission\Traits\HasRoles; class User extends Authenticatable { use HasFactory, Notifiable, HasRoles; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; } |
Step 5 – Register Middleware
Now we will register a middleare as following
1 2 3 4 5 6 |
protected $routeMiddleware = [ .... 'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class, 'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class, 'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class, ] |
Step 6 – Create Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\HomeController; use App\Http\Controllers\RoleController; use App\Http\Controllers\UserController; use App\Http\Controllers\ProductController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Auth::routes(); Route::get('/home', [HomeController::class, 'index'])->name('home'); Route::group(['middleware' => ['auth']], function() { Route::resource('roles', RoleController::class); Route::resource('users', UserController::class); Route::resource('products', ProductController::class); }); |
Step 7 – Create Controllers
Now, lets create some controller using command given below –
1 2 3 |
php artisan make controller UserController php artisan make controller RoleController php artisan make controller ProductController |
Then open UserController.php file and add the following code into it, which is placed on app/http/controllers directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Models\User; use Spatie\Permission\Models\Role; use DB; use Hash; use Illuminate\Support\Arr; 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::pluck('name','name')->all(); 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); $user->assignRole($request->input('roles')); 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::pluck('name','name')->all(); $userRole = $user->roles->pluck('name','name')->all(); return view('users.edit',compact('user','roles','userRole')); } /** * Update the specified resource in storage. * * @param \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 = Arr::except($input,array('password')); } $user = User::find($id); $user->update($input); DB::table('model_has_roles')->where('model_id',$id)->delete(); $user->assignRole($request->input('roles')); return redirect()->route('users.index') ->with('success','User updated successfully'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { User::find($id)->delete(); return redirect()->route('users.index') ->with('success','User deleted successfully'); } } |
Then open RoleController.php file and add the following code into it, which is placed on app/http/controllers directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Spatie\Permission\Models\Role; use Spatie\Permission\Models\Permission; use DB; class RoleController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ function __construct() { $this->middleware('permission:role-list|role-create|role-edit|role-delete', ['only' => ['index','store']]); $this->middleware('permission:role-create', ['only' => ['create','store']]); $this->middleware('permission:role-edit', ['only' => ['edit','update']]); $this->middleware('permission:role-delete', ['only' => ['destroy']]); } /** * Display a listing of the resource. * * @return \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', 'permission' => 'required', ]); $role = Role::create(['name' => $request->input('name')]); $role->syncPermissions($request->input('permission')); return redirect()->route('roles.index') ->with('success','Role created successfully'); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $role = Role::find($id); $rolePermissions = Permission::join("role_has_permissions","role_has_permissions.permission_id","=","permissions.id") ->where("role_has_permissions.role_id",$id) ->get(); return view('roles.show',compact('role','rolePermissions')); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $role = Role::find($id); $permission = Permission::get(); $rolePermissions = DB::table("role_has_permissions")->where("role_has_permissions.role_id",$id) ->pluck('role_has_permissions.permission_id','role_has_permissions.permission_id') ->all(); return view('roles.edit',compact('role','permission','rolePermissions')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $this->validate($request, [ 'name' => 'required', 'permission' => 'required', ]); $role = Role::find($id); $role->name = $request->input('name'); $role->save(); $role->syncPermissions($request->input('permission')); return redirect()->route('roles.index') ->with('success','Role updated successfully'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { DB::table("roles")->where('id',$id)->delete(); return redirect()->route('roles.index') ->with('success','Role deleted successfully'); } } |
Then open ProductController.php file and add the following code into it, which is placed on app/http/controllers directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
<?php namespace App\Http\Controllers; use App\Models\Product; use Illuminate\Http\Request; class ProductController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ function __construct() { $this->middleware('permission:product-list|product-create|product-edit|product-delete', ['only' => ['index','show']]); $this->middleware('permission:product-create', ['only' => ['create','store']]); $this->middleware('permission:product-edit', ['only' => ['edit','update']]); $this->middleware('permission:product-delete', ['only' => ['destroy']]); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $products = Product::latest()->paginate(5); return view('products.index',compact('products')) ->with('i', (request()->input('page', 1) - 1) * 5); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('products.create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { request()->validate([ 'name' => 'required', 'detail' => 'required', ]); Product::create($request->all()); return redirect()->route('products.index') ->with('success','Product created successfully.'); } /** * Display the specified resource. * * @param \App\Product $product * @return \Illuminate\Http\Response */ public function show(Product $product) { return view('products.show',compact('product')); } /** * Show the form for editing the specified resource. * * @param \App\Product $product * @return \Illuminate\Http\Response */ public function edit(Product $product) { return view('products.edit',compact('product')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Product $product * @return \Illuminate\Http\Response */ public function update(Request $request, Product $product) { request()->validate([ 'name' => 'required', 'detail' => 'required', ]); $product->update($request->all()); return redirect()->route('products.index') ->with('success','Product updated successfully'); } /** * Remove the specified resource from storage. * * @param \App\Product $product * @return \Illuminate\Http\Response */ public function destroy(Product $product) { $product->delete(); return redirect()->route('products.index') ->with('success','Product deleted successfully'); } } |
Step 8 – Install Laravel UI
In this step we will install laravel UI using following command:
1 |
composer require laravel/ui |
Step 9 – Install Bootstrap Auth Scaffolding
Now, we will install bootstrap auth scaffolding using following command:
1 |
php artisan ui bootstrap --auth |
Step 10 – Install Npm Packages
Now install npm dependencies using following command:
1 |
npm install |
Now, open terminal and type the following command on terminal to create tables in database:
1 |
php artisan migrate |
Step 11 – Create Blade Views
Now, In this step we will create following blade views file:
- Layout
- app.blade.php
- Users Module
- index.blade.php create.blade.php edit.blade.php show.blade.php
- Roles Module
- index.blade.php create.blade.php edit.blade.php show.blade.php
- Product Module
- index.blade.php create.blade.php edit.blade.php show.blade.php
Now, create and open app.blade.php file and add the following code into it, which is places inside resources/views/layouts directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<html lang="{{ app()->getLocale() }}"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- CSRF Token --> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>{{ config('app.name', 'Laravel 8 User Roles and Permissions Tutorial') }}</title> <!-- Scripts --> <script src="{{ asset('js/app.js') }}" defer></script> <!-- Fonts --> <link rel="dns-prefetch" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css?family=Raleway:300,400,600" rel="stylesheet" type="text/css"> <!-- Styles --> <link href="{{ asset('css/app.css') }}" rel="stylesheet"> </head> <body> <div id="app"> <nav class="navbar navbar-expand-md navbar-light navbar-laravel"> <div class="container"> <a class="navbar-brand" href="{{ url('/') }}"> Laravel 8 User Roles and Permissions </a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <!-- Left Side Of Navbar --> <ul class="navbar-nav mr-auto"></ul> <!-- Right Side Of Navbar --> <ul class="navbar-nav ml-auto"> <!-- Authentication Links --> @guest <li><a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a></li> <li><a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a></li> @else <li><a class="nav-link" href="{{ route('users.index') }}">Manage Users</a></li> <li><a class="nav-link" href="{{ route('roles.index') }}">Manage Role</a></li> <li><a class="nav-link" href="{{ route('products.index') }}">Manage Product</a></li> <li class="nav-item dropdown"> <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre> {{ Auth::user()->name }} <span class="caret"></span> </a> <div class="dropdown-menu" aria-labelledby="navbarDropdown"> <a class="dropdown-item" href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();"> {{ __('Logout') }} </a> <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;"> @csrf </form> </div> </li> @endguest </ul> </div> </div> </nav> <main class="py-4"> <div class="container"> @yield('content') </div> </main> </div> </body> </html> |
Then create users directory inside resources/views directory.
Now, create and open index.blade.php file and add the following code into it, which is placed on resources/views/users directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
@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->getRoleNames())) @foreach($user->getRoleNames() as $v) <label class="badge badge-success">{{ $v }}</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() !!} <p class="text-center text-primary"><small></small></p> @endsection |
Now, create and open create.blade.php file and add the following code into it, which is placed on resources/views/users directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
@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() !!} <p class="text-center text-primary"><small></small></p> @endsection |
Now, create and open edit.blade.php file and add the following code into it, which is placed on resources/views/users directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
@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() !!} <p class="text-center text-primary"><small></small></p> @endsection |
Now, create and open show.blade.php file and add the following code into it, which is placed on resources/views/users directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
@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->getRoleNames())) @foreach($user->getRoleNames() as $v) <label class="badge badge-success">{{ $v }}</label> @endforeach @endif </div> </div> </div> @endsection |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
@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->getRoleNames())) @foreach($user->getRoleNames() as $v) <label class="badge badge-success">{{ $v }}</label> @endforeach @endif </div> </div> </div> @endsection |
Now, create Roles directory inside resources/views directory.
Now, create and open index.blade.php file and add the following code into it, which is placed on resources/views/roles directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
@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"> @can('role-create') <a class="btn btn-success" href="{{ route('roles.create') }}"> Create New Role</a> @endcan </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 width="280px">Action</th> </tr> @foreach ($roles as $key => $role) <tr> <td>{{ ++$i }}</td> <td>{{ $role->name }}</td> <td> <a class="btn btn-info" href="{{ route('roles.show',$role->id) }}">Show</a> @can('role-edit') <a class="btn btn-primary" href="{{ route('roles.edit',$role->id) }}">Edit</a> @endcan @can('role-delete') {!! Form::open(['method' => 'DELETE','route' => ['roles.destroy', $role->id],'style'=>'display:inline']) !!} {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!} {!! Form::close() !!} @endcan </td> </tr> @endforeach </table> {!! $roles->render() !!} <p class="text-center text-primary"><small></small></p> @endsection |
Now, create and open create.blade.php file and add the following code into it, which is placed on resources/views/roles directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
@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>Permission:</strong> <br/> @foreach($permission as $value) <label>{{ Form::checkbox('permission[]', $value->id, false, array('class' => 'name')) }} {{ $value->name }}</label> <br/> @endforeach </div> </div> <div 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() !!} <p class="text-center text-primary"><small></small></p> @endsection |
Now, create and open edit.blade.php file and add the following code into it, which is placed on resources/views/roles directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
@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('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>Permission:</strong> <br/> @foreach($permission as $value) <label>{{ Form::checkbox('permission[]', $value->id, in_array($value->id, $rolePermissions) ? true : false, array('class' => 'name')) }} {{ $value->name }}</label> <br/> @endforeach </div> </div> <div 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 <p class="text-center text-primary"><small></small></p> |
Now, create and open show.blade.php file and add the following code into it, which is placed on resources/views/roles directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
@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->name }} </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->name }},</label> @endforeach @endif </div> </div> </div> @endsection |
Then create Product directory inside resources/views directory.
Now, create and open index.blade.php file and add the following code into it, which is placed on resources/views/product directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Products</h2> </div> <div class="pull-right"> @can('product-create') <a class="btn btn-success" href="{{ route('products.create') }}"> Create New Product</a> @endcan </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>Details</th> <th width="280px">Action</th> </tr> @foreach ($products as $product) <tr> <td>{{ ++$i }}</td> <td>{{ $product->name }}</td> <td>{{ $product->detail }}</td> <td> <form action="{{ route('products.destroy',$product->id) }}" method="POST"> <a class="btn btn-info" href="{{ route('products.show',$product->id) }}">Show</a> @can('product-edit') <a class="btn btn-primary" href="{{ route('products.edit',$product->id) }}">Edit</a> @endcan @csrf @method('DELETE') @can('product-delete') <button type="submit" class="btn btn-danger">Delete</button> @endcan </form> </td> </tr> @endforeach </table> {!! $products->links() !!} <p class="text-center text-primary"><small></small></p> @endsection |
Now, create and open create.blade.php file and add the following code into it, which is placed on resources/views/product directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Add New Product</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a> </div> </div> </div> @if ($errors->any()) <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 action="{{ route('products.store') }}" method="POST"> @csrf <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> <input type="text" name="name" class="form-control" placeholder="Name"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Detail:</strong> <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"></textarea> </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> <p class="text-center text-primary"><small></small></p> @endsection |
Now, create and open edit.blade.php file and add the following code into it, which is placed on resources/views/product directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Edit Product</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a> </div> </div> </div> @if ($errors->any()) <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 action="{{ route('products.update',$product->id) }}" method="POST"> @csrf @method('PUT') <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> <input type="text" name="name" value="{{ $product->name }}" class="form-control" placeholder="Name"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Detail:</strong> <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail">{{ $product->detail }}</textarea> </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> <p class="text-center text-primary"><small></small></p> @endsection |
Now, create and open show.blade.php file and add the following code into it, which is placed on resources/views/product directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2> Show Product</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('products.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> {{ $product->name }} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Details:</strong> {{ $product->detail }} </div> </div> </div> @endsection <p class="text-center text-primary"><small></small></p> |
Step 12 – Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000/ |