1

i am new to laravel and i am trying to make an admin dashboard but in the adminmiddleware file i am having this error with both of my Auth

Undefined type 'App\Http\Middleware\Auth'
namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class AdminMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        if (Auth::check()) {
            if (Auth::user()->role_as == '1') {
                return $next($request);
            } else {
                return redirect('/home')->with('status', 'Access denied! as you are not an admin');
            }
        } else {
            return redirect('/home')->with('status', ' pleaselogin first');
        }
    }
}
1
  • 2
    The answers below have it; namespacing issue. But, there's also a global method auth() that does the same thing; auth()->user() is equivalent to Auth::user() and doesn't require importing the Auth facade. Commented Feb 8, 2022 at 16:19

2 Answers 2

2

add

use Illuminate\Support\Facades\Auth;

before

use Illuminate\Http\Request;
Sign up to request clarification or add additional context in comments.

Comments

0

either use the Auth facade from the global namespace: \Auth,

or (better) add its FQCN in your use declarations: use Illuminate\Support\Facades\Auth;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.