Laravel: how to check middleware in blade template

laravel middleware
laravel gate middleware
laravel middleware redirect
laravel route group(('middleware))
laravel middleware in controller
laravel middleware except
laravel middleware multiple roles
laravel blade

i've created Middleware

php artisan make:middleware isTeacher

in App/Http/isTeacher.php i've placed the check:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class isTeacher
{
  public function handle($request, Closure $next)
  {
    $user = Auth::user();

    if($user && $user->capability == 3)
    {
        return $next($request);
    }
    else
        return redirect('/login');
  }
}

than, i've defined the middleware in app/Http/Kernel.php

protected $routeMiddleware = [
    ...
    'auth.teacher' => \App\Http\Middleware\isTeacher::class,
    ...
];

The question is: how I check the teacher capability in blade template? I'm trying with this:

@if (Auth::isTeacher())

but doesn't work

any help are appreciated


The blade directive only needs to return true or false nothing else.

You could just do the following as in your boot() method of the AppServiceProdiver.php

Blade::if('teacher', function () {
    return auth()->user() && $user->capability == 3;
});

How to use middleware in blade ?, Hello , I would like to use some middlewares in the blade view now in sidebar.​blade.php I would like to show the related links to the [Vue warn]: Error compiling template: The value for a v-bind expression cannot be empty. Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.


Well there is an issue here Middlewares are used to filter HTTP requests or its outter layer of the onion in laravel app. It is not defined to be used in blade to decide which part of html should be rendered.

You can filter a controller function to only be usable if it passes the middleware (if authenticated for example) and the controller only runs the function if it passes on the middleware.

When using Auth in blades you are not using a middleware but a facade whan you could do its use Session for your case like:

In your middleware

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class isTeacher
{
  public function handle($request, Closure $next)
  {
    $user = Auth::user();

    if($user && $user->capability == 3)
    {
        \session('isTeacher', true);
        return $next($request);
    }

    return redirect('/login');

}

Then in your blades do like:

@if (Session::get('isTeacher', 0));

This will show content only to teachers, if session its not set it will fall back to 0 or false.

Middleware - Laravel, For example, Laravel includes a middleware that verifies the user of your For example, if your application needs to verify that the authenticated user has a  A CORS middleware might be responsible for adding the proper headers to all responses leaving your application. A logging middleware might log all incoming requests to your application. There are several middleware included in the Laravel framework, including middleware for authentication and CSRF protection.


In this scenario, using middleware is not really the solution. I will rather suggest blade directive, which allows you to create a custom blade function like:

@author
    //Do something
@else
    //Something else
@endauthor

To use the above syntax, you can register a new blade directive in your AppServiceProvider.php file

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Blade::directive('author', function () {
            $isAuth = false;

            // check if the user authenticated is teacher
            if (auth()->user() && auth()->user()->capability == 3) {

                $isAuth = true;
            }

            return "<?php if ($isAuth) { ?>";
        });

        Blade::directive('endauthor', function () {
            return "<?php } ?>";
        });
    }
}

After the above changes in the AppServiceProvider.php file, run php artisan view:clear

For a better understanding, you can refer to the documentation here

Authorization - Laravel, Via The User Model; Via Middleware; Via Controller Helpers; Via Blade Templates Gates are Closures that determine if a user is authorized to perform a given  Laravel 5.1 introduces the concept of using Blade, a templating engine to design a unique layout. The layout thus designed can be used by other views, and includes a consistent design and structure. The layout thus designed can be used by other views, and includes a consistent design and structure.


This is an improved version of Prince Lionel's answer. Pay attention to the return value of Blade::directive callback function.

@author
    //Do something
@else
    //Something else
@endauthor

AppServiceProvider.php

namespace App\Providers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Blade::directive('author', function () {
            $isAuth = false;

            // check if the user authenticated is teacher
            if (auth()->user() && auth()->user()->capability == 3) {

                $isAuth = true;
            }

            return "<?php if (" . intval($isAuth) . ") { ?>";
        });

        Blade::directive('endauthor', function () {
            return "<?php } ?>";
        });
    }
}

Create Custom Laravel Blade Directive & Middleware Easily, In blade template for that I have to write many code. I think that will be messy. In this tutorial, I will make a middleware and a @admin  I am in the process of trying out Laravel Livewire with a project for the first time. I am trying to build a fairly simple form with one set of cascading dropdowns – one input dependent on the other. But, when I try to pass an eloquent object to a component property, it is converted to a string so I can not access any of the object’s


Laravel: how to check middleware in blade template, Laravel: how to check middleware in blade template. i've created Middleware php artisan make:middleware isTeacher. LARAVEL – Project 3 - занятие №13 (Custom login/registration в LARAVEL на template Messages) - Duration: 1:08:09. Alexander Koskin 12 views 1:08:09


MULTI-AUTH SYSTEM WITH BLADE TEMPLATING IN LARAVEL 6, MULTI-AUTH SYSTEM WITH BLADE TEMPLATING IN LARAVEL 6 Here, we need to mention our created guards through middleware in the Auth public function showAdminLoginForm() { return view('auth.login', ['url'  Integrate Bootstrap Template with Laravel Use Laravel Blade Template Engine Working With Controllers and Middleware in Laravel Understanding Models and Views in Laravel Laravel Many To Many Relationship Tutorial Example How to use PUT/PATCH and DELETE method in Laravel Laravel Elasticsearch Tutorial Example From Scratch Laravel Eloquent One To


Laravel - Middleware, Laravel - Middleware - Middleware acts as a bridge between a request and a response. Laravel - Views · Laravel - Blade Templates · Laravel - Redirections · Laravel - Working Observe the following example to understand the middleware mechanism − \Illuminate\View\Middleware\​ShareErrorsFromSession::class,  By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the App\Http\Kernel class. These middleware will automatically trim all incoming string fields on the request, as well as convert any empty string fields to null. This