加速laravel中间件代码中的重定向

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class isAdmin
{


    public function __construct(Guard $auth) {
        $this->auth = $auth;
    }


    /**
    * Handle an incoming request.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  \Closure  $next
    * @return mixed
    */
    public function handle($request, Closure $next)
    {
        $user = auth()->user();

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

        abort(403, 'You do not have permission to perform this action.');

    }

this is in my user.php model

protected function roleId(){ 
    return auth()->user()->role_id;
}


public function isAdmin()
{
    if (  $this->roleId() === 4 || $this->roleId() === 6 ) { 
        return true ;
    }
    else {
        return false ; 
    }
}

this is in my routes/web.php file

Route::group([
    'namespace' => 'Admin',
    'middleware' => 'isAdmin',
    'prefix' => 'admin'
], function () {

    Route::get('/', 'DashboardController@index')->name('admin.dashboard');


});

But it's slow as hell, can I improve it's speed, I a definitely redirected but it's slow asf, and I want to improve its speed, maybe some optimization can do. Any tips from you guys..

<?php 
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;

ini_set('max_execution_time', 180);
class DashboardController extends Controller { 


    public function index()
    {
        return view('app.admin.dashboard');
    }
}

edited the code to see what's in the dashboard view

I would say there are some scopes for improvement in your current code as well.

Your User model seems to have nested functions. e.g. middleware is calling isAdmin() and its then calling roleId() then that is doing auth()->user()->role_id and not $this->role_id.

Instead of having middleware, use policy may be e.g. DashboardPolicy.

Your policy can have the method :

public function index(User $user)
{
   return in_array($user->role_id, [4, 6]);
}

Then Register it inside app/Providers/AuthServiceProvider.php:

protected $policies = [
.
.
.
Document::class => DocumentPolicy::class,
]

and then in your controller you will do

public function index(){

    $this->authorize('index', Dashboard::class);
    return view('app.admin.dashboard');

}

This will be cleaner.

Now to debug the time issue, you can comment all custom middlewares and just keep it simple returning view. If it is fast then one of your custom middleware was causing this. If you implement the policy approach, isAdmin middleware can be removed directly.