Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Condition in laravel 5 Middleware always false

I'm trying to give condition in my Middleware.

Here is my script

if (auth()->check() && auth()->user()->type == 'TP001') {
    $menu->add("User Control",array('nickname' => "user",'class'=>'treeview'))
    ->append(' <b class="caret"></b>')
    ->prepend('<span class="glyphicon glyphicon-user"></span> ');

    $menu->user->add('Daftar User','user/list');
    $menu->user->add('Tipe User','user/type');
} else {
    /* Some code here...*/
}

The script above I cants see the menu with the condition even I already login with 'TP001' (always in else), then I try to fix my code with this

auth()->user()->isDeveloper()

My model

public function isDeveloper()
{
    return ($this->type == 'TP001');
}

But still not working, is there any way to give the condition like above but in a correct way? Thanks in advance and sorry for my bad English.

My Kernel

  protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\Frontend::class,
  ];
like image 380
YVS1102 Avatar asked Jul 18 '17 03:07

YVS1102


People also ask

What is default middleware in Laravel?

By default, the $routeMiddleware property of this class contains entries for the middleware included with Laravel. You may add your own middleware to this list and assign it a key of your choosing: // Within App\Http\Kernel class...

How does middleware work in Laravel?

Laravel Middleware acts as a bridge between a request and a reaction. It is a type of sifting component. Laravel incorporates a middleware that confirms whether or not the client of the application is verified. If the client is confirmed, it diverts to the home page otherwise, it diverts to the login page.

How many types of middleware are there in Laravel?

There are two types of Middleware in Laravel. The Global Middleware will run on every HTTP request of the application, whereas the Route Middleware will be assigned to a specific route. The middleware can be registered at app/Http/Kernel.

What is middleware in Laravel explain your answer with example?

Middleware provide a convenient mechanism for filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen.


1 Answers

The middleware kernel has that $middleware you posted, with middleware that run in every request, but they run before the route middleware (which you select in the routes definition).

You are probably using the "web" middleware group. Try adding your custom middleware at the end. I think the default in Laravel 5.4 is:

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \App\Http\Middleware\Frontend::class, // <-- your middleware at the end
    ],
    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

This way you know your middleware will run after the others (the one that starts the session and checks authentication)

like image 89
alepeino Avatar answered Sep 30 '22 10:09

alepeino