Swap DEFAULT table from Laravel Framework Auth 5.8.15

Asked

Viewed 496 times

1

Good evening, I would like to know how to use the auth of Laravel but with a different table, I saw some articles on the internet but could not change yet.

I tried to overwrite the protected method $table = 'table_name'; But unsuccessfully, he did not authenticate there, someone could help me?

Model of access to the bank:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $table = 'autenticar';

    protected $fillable = [
        'email', 'password_hash',
    ];

    protected $username = 'email';

    protected $email = 'email';

    protected $password = 'password_hash';


    // protected $hidden = [
    //     'password', 'remember_token',
    // ];

    // protected $casts = [
    //     'email_verified_at' => 'datetime',
    // ];
}
  • why change table name? and which fields exist in this table? because that protected ai $username, $email????

1 answer

0

I was able to solve by creating a new model and overriding some methods and variables of the Auth standard of Laravel:

Nova Model:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User2 extends Authenticatable
{
    use Notifiable;

    protected $table = 'udropship_vendor';
    protected $primaryKey = 'vendor_id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'email', 'password_hash'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password_hash'
    ];

    public function getAuthPassword()
    {
        return bcrypt($this->password_hash);
    }
}

And in the config/Auth.php file I overwrote providers for my new model, follow the code:

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User2::class,
        ],

Another very important detail and I lost good hours trying to solve, the login pattern of Auth Laravel when sending the password it makes a bcrypt in the password, as my bank had password type MD5 I took to find the problem, i ended up creating a method to read and change the password type as you can see in my model, method:

public function getAuthPassword()
    {
        return bcrypt($this->password_hash);
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.