Problem in custom auth 7

Asked

Viewed 98 times

-2

I’m trying to set up a custom table for auth in 7.x. Login works normally, but a new user record returns the error: "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'Rh.users' doesn’t exist (SQL: select Count(*) as Aggregate from users Where cpf = 000000)". The "users" table was replaced by "users" and I made the changes in auth.php, I changed the $table in the model and the create function within the controller, but the error is still displayed.

My model is like this:

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Usuarios extends Authenticatable
{
    protected $table = "usuarios";
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'nome',
        'cpf',
        'email',
        'telefone',
        'endereco',
        'numero',
        'complemento',
        'bairro',
        'cidade',
        'estado',
        'cep',
        'celular',
        'password'
    ];

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

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

My controller is like this:

middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'nome' => ['required', 'string', 'max:255'],
            'cpf' => ['required', 'string', 'max:255', 'unique:users'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'telefone' => ['string', 'max:255'],
            'endereco' => ['required', 'string', 'max:255'],
            'numero' => ['required', 'string', 'max:255'],
            'complemento' => ['string', 'max:255'],
            'bairro' => ['required', 'string', 'max:255'],
            'cidade' => ['required', 'string', 'max:255'],
            'estado' => ['required', 'string', 'max:255'],
            'cep' => ['required', 'string', 'max:255'],
            'celular' => ['string', 'max:255'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\Models\Usuarios
     */
    protected function create(array $data)
    {
        return Usuarios::create([
            'nome' => $data['nome'],
            'cpf' => $data['cpf'],
            'email' => $data['email'],
            'telefone' => $data['telefone'],
            'endereco' => $data['endereco'],
            'numero' => $data['numero'],
            'complemento' => $data['complemento'],
            'bairro' => $data['bairro'],
            'cidade' => $data['cidade'],
            'estado' => $data['estado'],
            'cep' => $data['cep'],
            'celular' => $data['celular'],
            'senha' => Hash::make($data['password']),
        ]);
    }
}

And my auth.php is like this:

 [
        'guard' => 'web',
        'passwords' => 'usuarios',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'usuarios',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'usuarios',
            'hash' => false,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'usuarios' => [
            'driver' => 'eloquent',
            'model' => App\Models\Usuarios::class
        ],

        /* 'usuarios' => [
            'driver' => 'database',
            'table' => 'usuarios',
        ], */
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'usuarios' => [
            'provider' => 'usuarios',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Password Confirmation Timeout
    |--------------------------------------------------------------------------
    |
    | Here you may define the amount of seconds before a password confirmation
    | times out and the user is prompted to re-enter their password via the
    | confirmation screen. By default, the timeout lasts for three hours.
    |
    */

    'password_timeout' => 10800,

];
  • the method Create there is an error at the end with the password field that does not exist? at least not in the model. At which point this table error occurs users?

  • Your question seems to have some problems and your experience here in Stack Overflow may not be the best because of it. We want you to do well here and get what you want, but for that we need you to do your part. Here are some guidelines that will help you: Stack Overflow Survival Guide in English (short version). If the solution is very simple it is still possible for someone to do so in the comments.

1 answer

0


There are two errors in your code one of them in the validation method where you placed the table users which is actually usuarios in the validation unique of cpf and email example as it should be:

protected function validator(array $data)
{
    return Validator::make($data, [
        'nome' => ['required', 'string', 'max:255'],
        'cpf' => ['required', 'string', 'max:255', 'unique:usuarios'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:usuarios'],
        'telefone' => ['string', 'max:255'],
        'endereco' => ['required', 'string', 'max:255'],
        'numero' => ['required', 'string', 'max:255'],
        'complemento' => ['string', 'max:255'],
        'bairro' => ['required', 'string', 'max:255'],
        'cidade' => ['required', 'string', 'max:255'],
        'estado' => ['required', 'string', 'max:255'],
        'cep' => ['required', 'string', 'max:255'],
        'celular' => ['string', 'max:255'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

and in the Create because the countryside is not senha and yes password as described in its model Usuarios:

protected function create(array $data)
{
    return Usuarios::create([
        'nome' => $data['nome'],
        'cpf' => $data['cpf'],
        'email' => $data['email'],
        'telefone' => $data['telefone'],
        'endereco' => $data['endereco'],
        'numero' => $data['numero'],
        'complemento' => $data['complemento'],
        'bairro' => $data['bairro'],
        'cidade' => $data['cidade'],
        'estado' => $data['estado'],
        'cep' => $data['cep'],
        'celular' => $data['celular'],
        'password' => Hash::make($data['password']),
    ]);
}

are typos.

  • 1

    It worked, now ta 100%, I had not even noticed those details, thanks even

Browser other questions tagged

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