V
V
Vova1357982022-01-26 20:21:04
Laravel
Vova135798, 2022-01-26 20:21:04

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

3 answer(s)
S
Sergey delphinpro, 2022-01-26
@Vova135798

public function handle(Request $request, Closure $next)
{
  if (Auth::guest() || Auth::user()->admin()==0){
    return redirect()->route('index');
  }
  return $next($request);
}

T
Timur Maslov, 2022-01-28
@tmaslov22

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 question

Ask a Question

731 491 924 answers to any question