Answer the question
In order to leave comments, you need to log in
How to solve Call to a member function admin() on null error?
I'm making an admin page. Created middleware and registered check, added routes. But there is an error in validation: 'Call to a member function admin() on null'
public function handle(Request $request, Closure $next)
{
if(Auth::user()->admin()==0){
return redirect()->route('index');
}
return $next($request);
}
Route::get('/admin', [App\Http\Controllers\AdminController::class, 'admin'])->middleware('isadmin');
Answer the question
In order to leave comments, you need to log in
public function handle(Request $request, Closure $next)
{
if (Auth::guest() || Auth::user()->admin()==0){
return redirect()->route('index');
}
return $next($request);
}
It checks whether the user is an admin, but not the fact that the user is logged into his account. This case needs to be taken into account.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question