Majeed
197
5
Laravel

Laravel Middleware doesn't work properly.

Posted 10 months ago by Majeed

I have a role middleware. when i pass to role from my controller as a string it's work properly but when pass as an array it's not working. This way middleware work properly.

$this->middleware('HasRole:User|Admin|Author')->except(['userEdit','roleEdit','permissionEdit','userUpdate']);

But when I change like this, it's not working.

    $this->middleware('HasRole:User,Admin,Author')->except(['userEdit','roleEdit','permissionEdit','userUpdate']);

Here, is my middleware.

public function handle($request, Closure $next, ...$role)
    {
      
        $roles=is_array($role)? $role: explode('|', $role);
        //dd($roles);

        if($request->user()===null)
        {
            return response('Insufficient Access',401);
        }

        if($request->user()->hasAnyRole($roles) || !$roles)
        {
             return $next($request);
        }
          return response('Insufficient Permission',401);
        //return $next($request);
    }

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.