Laravel 5.2 auth change 'users' table

六眼飞鱼酱① 提交于 2019-12-07 03:56:39

问题


I used the new feature in Laravel:

 php artisan make:auth

But when I register it will use the database table users by default, but I want to change that to an other table. And by default it uses updated_at and created_at in that table, I want to remove that too.

Auth/AuthController

protected function create(array $data)
{
    return User::create([
        'voornaam' => $data['voornaam'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

app\User

protected $fillable = [
    'voornaam', 'email', 'password',
];

This are the things I thought would change it, but they didn't. I hope somebody can tell me some more about this issue.


回答1:


To change table name go to app/User.php and set property $table to custom one for example:

$table = 'new_table';

You should also change default migration. Go to: /database/migrations/2014_10_12_000000_create_users_table.php file and change users here for the same name. To remove timestamps you can remove line:

$table->timestamps();

however if I were you I would reconsider removing those timestamps




回答2:


DO NOT FORGET TO CHANGE VALIDATION IN REGISTERCONTOLLER.PHP AS WELL.

from

'email' => 'required|email|max:255|unique:users',

to

'email' => 'required|email|max:255|unique:company',



回答3:


By default model take its class name as table name !
I define a protected property at the top of App/User.php

protected $table = 'auth_users';

This tells laravel to use auth_users table instead of default user table. and it works like charm.




回答4:


Check Laravel 5.3 Change user table in Auth for an alternative solution, using modification in config/auth.php and RegisterController.php.

To disable the timestamps you may also use in the model class:

public $timestamps = false;


来源:https://stackoverflow.com/questions/34664087/laravel-5-2-auth-change-users-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!