skip to Main Content

this is my login logic when i try it give me error down blow

 public function login(Request $request)
    {
        $request->validate([
            'Email'=>'required',
            'password'=> 'required'
        ]);

        $agent =  DB::table('agents')->where('Email',$request->Email)->first();


        if($request->password == $agent->password || Hash::check($request->password,$agent->password))
        {
            Auth::login($agent);
            return redirect()->route('agent.index');
        }
        
        else 
        {
            return dd('no');
        }

The Error :

IlluminateAuthSessionGuard::login(): Argument #1 ($user) must be of type IlluminateContractsAuthAuthenticatable, AppModelsagent given, called in C:xamppshtdocsLaravelLaravel-10REVvendorlaravelframeworksrcIlluminateAuthAuthManager.php on line 340

i new to laravel so i did not try much cause i know very little about authentication and login logic also how to deal with middleware
so please if u dont have answer to my question i need help on from where i could know more about auth and middleware and deg deeper in Laravel beside the documentation

2

Answers


  1. You can’t use the agents model or the table to authenticate users straight out of the box.

    According to the Laravel documentation:

    By default, Laravel includes an AppModelsUser Eloquent model in your app/Models directory. This model may be used with the default Eloquent authentication driver.

    This means that Laravel uses the User model to authenticate users. So, you could stick with the User model that Laravel comes with or customize the default authentication driver to fit your needs. I’d recommend sticking with the default authentication driver, but it’s ultimately up to you.

    You can take a look at these references for how to create a custom authentication guard.

    Laravel official documentation

    How to use auth guard in laravel

    Login or Signup to reply.
  2. I’m not entirely sure why you’re searching for the Agent model first, but you should follow these steps:

    After retrieving your agent, you need to find the User model because the login method expects an object of the User model.

    public function login(Request $request)
    {
        $request->validate([
            'Email' => 'required',
            'password' => 'required',
        ]);
    
        // Retrieve the agent
        $agent = DB::table('agents')->where('Email', $request->Email)->first();
    
        // Retrieve the user associated with the agent
        $user = User::where('id', $agent->user_id)->first();
    
        // Log in the user
        Auth::login($user);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search