Laravel middleware Multiple Roles and Parameters Example

Today, We want to share with you Laravel middleware Multiple Roles and Parameters Example.In this post we will show you Multiple middleware on routing group, hear for php – Laravel middleware with multiple roles we will give you demo and example for implement.In this post, we will learn about Middleware parameters – comma separated with an example.

Laravel middleware Multiple Roles and Parameters Example

There are the Following The simple About Laravel middleware Multiple Roles and Parameters Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel middleware multiple roles pass, so the some laravel middleware parameters array/laravel auth middleware redirect for this example is following below.

  • Define Middleware in Kernel
  • Laravel Middleware
  • Define Laravel Routes with middleware
Read Also:  Define Custom Laravel Validation Rules Example

Step 1: Define Middleware in Kernel

Http\Kernel.php

protected $routeMiddleware = [
    ...
    'role' => \App\Http\Middleware\Role::class,
];

Step 2: Laravel Middleware

Http\Middleware\Role.php

<?php
//php - Laravel middleware with multiple roles

namespace App\Http\Middleware;

use Closure;
use Auth;// on The top

public function handle($request, Closure $next, ... $roles)
{
    if (!Auth::check()) // I Added this check so We have it, Therefor it really need be part of your create a realtime laravel authentication 'auth' middleware in laravel, most simply included as part of a laravel web file to call route group.
        return redirect('login');

    $user = Auth::user();

    if($user->isAdmin())
        return $next($request);

    foreach($roles as $role) {
        // here conditions Check if real time user has the role This check will your depend on how your roles (perfect true) are set up
        if($user->hasRole($role))
            return $next($request);
    }

    return redirect('login');
}

Step 3: Define Laravel Routes with middleware

routes/web.php

Read Also:  Create Authentication Custom Middleware in Laravel 5.7

Finally in your Laravel web.php in routes

Route::get('admin/memberen/overzicht', '[email protected]')->middleware('role:teacher,student');
Route::get('admin/memberen/{id}/bewerken', '[email protected]')->middleware('role:admin');

Angular 6 CRUD Operations Application Tutorials

Read :

Related Search : laravel middleware parameters array,laravel roles middleware,laravel middleware in controller,laravel multiple middleware,laravel middleware auth,laravel api middleware group,laravel middleware return response,laravel auth middleware redirect, Middleware parameters – comma separated and Multiple middleware on routing group.

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

Read Also:  PHP Laravel Middleware Route Groups

I hope you get an idea about Laravel middleware Multiple Roles and Parameters Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP Laravel 5.7 Image Upload with Validation Tutorial Example
  2. Read Also:  PHP Laravel Paths Example Tutorial From Scratch
  3. Read Also:  HTML PHP Ajax Progress Bar
  4. Read Also:  Multidimensional arrays in Laravel
  5. Read Also:  difference between php and html
  6. Read Also:  Laravel 6 Auth Redirection Using redirectTo
  7. Read Also:  Laravel Route Group Middleware Auth
  8. Read Also:  Laravel 6 Custom Middleware Tutorial With Example
  9. Read Also:  Laravel Auto redirect if not logged in
  10. Read Also:  Laravel 6 Eloquent Right JOIN Query Example Tutorial
CLOSEX