phplaraveldatabase

Laravel Auth::attempt() error: "Unknown column 'password'" when using a custom password field


I'm working on a Laravel project to migrate the project from codeigniter to laravel so i'm using a multi guard authentication and custom admin user table named tbl_admin_user, where both email and password column is named admin_user_email_id and admin_user_password instead of the default password.

But currently i'm facing this error - Where the field name is not mapping i guess plus I don't know why it's even passing the update query and the hashed password is not similar with the password which i have in my DB. The error highlights on the attempt() method.. Error

I have a custom guard admin and a custom Office model that extends Authenticatable. Here's what I’ve done: Model


namespace App\Models;

use Illuminate\Support\Facades\Hash;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Office extends Authenticatable
{

    use HasFactory, Notifiable;

    protected $table = 'tbl_admin_user';
    protected $primaryKey = 'admin_user_id';
    public $incrementing = true;
    public $keyType = 'int';

    protected $fillable = [
        // other column names
        'admin_user_email_id',
        'admin_user_password',
       // other column names
        'admin_updated_on',
        'admin_created_on',
    ];

    // Use customized field name
    public function getAuthIdentifierName() {
        return 'admin_user_email_id';
    }
    
    // Gets the hashed password
    public function getAuthPassword() {
        return $this->admin_user_password;
    }


    public $timestamps = false;

}

Login function from the controller

// Begin::Authenticate login
    public function authenticateOfficeLogin(Request $request) {
        // validate
        $validate = $request->validate([
            'admin_user_email_id' => 'required|email',
            'admin_user_password' => 'required'
        ]);
        
        // Store credentials
        $credentials = [
           'admin_user_email_id' => $validate['admin_user_email_id'],
           'password' => $validate['admin_user_password'],
           'admin_user_status' => 1,
        ];
        
        $user = Office::where('admin_user_email_id', $request->admin_user_email_id)->first(); // Gets the matching data

       
        // Check if user is authenticated
        if($user) {
            $authenticate = Auth::guard('admin')->attempt($credentials); // Here i got the error
            // if authenticated
                if($authenticate) {
                    return redirect()->route('office.dashboard')->with('success','Logged In Successfully!'); // redirect to the dashboard
                }
            }
        }
    // End::Authenticate login

What i've tried : Cleared configuration and route cache using php artisan config:clear, route:clear, and cache:clear and even tried optimize:clear.

Added Log::info() statements before the Auth::attempt() call, but nothing gets logged.

Verified that the provided password matches the hashed password using Hash::check() inside dd() before the attempt() call, and it returned true for now.


Solution

  • The error occurs because Laravel's Auth::attempt() expects a password key in credentials, but your database uses admin_user_password.

    In controller Update this code

    $credentials = [
        'admin_user_email_id' => $request->admin_user_email_id,
        'password' => $request->admin_user_password,
        'admin_user_status' => 1,
    ];