9

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,
  ];
alepeino
  • 9,551
  • 3
  • 28
  • 48
YVS1102
  • 2,658
  • 5
  • 34
  • 63

3 Answers3

13

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)

alepeino
  • 9,551
  • 3
  • 28
  • 48
3

You can put your custom middleware in protected $routeMiddleware = [ ] array in kernel file like this

$routeMiddleWare = ['frontend' => \App\Http\Middleware\Frontend::class]

After this you will be able to access your Auth::check and don't forget to put

Route::group(['middleware' => ['frontend']], function() { // your routes will go here.. });
2

You can get current user in any blade file like this:

{{ Auth::user()->name }}

In your blade do this:

@if(Auth::check() && Auth::user()->type == 'TP001')
     /* Some code here...*/
@endif

In your middleware do this as per your requirement!:

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...*/ 
}
Hiren Gohel
  • 4,942
  • 6
  • 29
  • 48