Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

coderic's avatar

Middleware to show items based on user roles

So I will have this page that will display a list of items based on the user's role. If the user is an admin, it'll display all the lists... if the user isn't an admin, it'll display only stuff that has a relation to their account.

I'm not sure if middleware would be the proper way to go about this and if it is.. how would I go about doing this?

I'll be using the same page for displaying the list of items for admin and non-admins.

0 likes
10 replies
mstnorris's avatar

Yes it is the best way. Full documentation on Middleware.

I have included a basic implementation and requires that you add an admin column to your users table. The example below will get you started.

  1. The following command creates new Middleware called Admin
php artisan make:middleware Admin
  1. This creates a file called Admin.php within the app/Http/Middleware directory that looks like
<?php namespace App\Http\Middleware;

use Closure;

class Admin {

    public function handle($request, Closure $next)
    {

        if ( Auth::check() && Auth::user()->isAdmin() )
        {
            return $next($request);
        }

        return redirect('home');

    }

}
  1. You then need to add the Admin Middleware to your app/Http/Kernel.php file
protected $routeMiddleware = [
    'auth' => 'App\Http\Middleware\Authenticate',
    'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
    'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
    'admin' => 'App\Http\Middleware\Admin', // this line right here
];
  1. Add the Admin Middleware to a route. (Within your routes.php file).
get('protected', ['middleware' => ['auth', 'admin'], function() {
    return "this page requires that you be logged in and an Admin";
}]);
  1. Finally you need to add the isAdmin method we created above to your User model to check whether or not the user is an Admin.
public function isAdmin()
{
    return $this->admin; // this looks for an admin column in your users table
}
  1. This will do the trick. If you run into any problems, please post what you have tried and which step you got up to and I'll try my best to help.
5 likes
mstnorris's avatar

@coderic Laracasts is having some issues at the moment, when it is back up and running I'll paste some code to get you started. Or, if you look at my profile on here, look at my recently "contributed to" section and you'll see that I wrote a complete walkthrough on using Middleware which you'll be able to use.

Update

So Laracasts is back up and running.

Within your view check if the user is an admin and show items accordingly.

@if ( $user->isAdmin )
    ...
@else
    ...
@endif
2 likes
coderic's avatar

@mstnorris How would this be implemented on a page like mine?

I have a page that has a dropdown list. It will display everything if you're an admin otherwise it will display things only related to you. If I go by your suggestion.. do I have to make a new view for both admin and non-admin users to display list content?

rvabrazaldo's avatar

sorry to necro, im having trouble, it says on my custom middleware that Class 'App\Http\Middleware\Auth' not found

Snapey's avatar

@rvabrazaldo probably the result of copying some 2-year+ old code

A LOT has changed since then - particularly with authentication.

Suggest you start a new thread.

1 like
woobarfoobar's avatar

From my understanding this should display different views depending on if the user is admin or not. But why create multiple views if the only difference is a couple of items in a list?

Wouldn't it make more sense to just have a single view and check the user role only in the controller or view?

Please or to participate in this conversation.