将"users"表的名称更改为帐户 Laravel 5

  • 本文关键字:Laravel users laravel
  • 更新时间 :
  • 英文 :


对Laravel来说相当陌生,希望遵循我习惯的表格惯例。

帐户的默认表名是"users",我想将其更改为"account。"如果有任何更改,我想更改为"user"并删除复数。

我已经使用migrate制作了一个名为"account"的克隆用户表,我只是想弄清楚我对现有代码所要做的一切,以使其能够登录

看起来我必须以某种方式更新"app/Http/Auth/AuthController.php",但我不确定我必须做什么…

我需要:

  • 更新"use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;"
  • 在AuthController中将"returnUser::create"更新为"returnAccount::create?如果是,我是否需要转到代码中创建类User的其他地方

我想另一种选择是废除他们的AuthController,建立我自己的AuthController并调用一个新的Account对象。。。这是我应该走的路吗?

如果您想将模型命名为Account:,我只需扩展User类并否决一些事情

编辑Account类中的表属性,请参阅:https://github.com/laravel/laravel/blob/master/app/User.php#L24

Account extends User {
    protected $table = 'accounts';
}

创建类帐户后,编辑配置的身份验证类,请参阅:https://github.com/laravel/laravel/blob/master/config/auth.php#L31

如果您只想否决User使用的表,请编辑User类:

protected $table = 'accounts';

老实说,为什么要麻烦?Taylor为您提供了这个框架来启动您的应用程序,为什么不使用它呢?尤其是如果您是Laravel的新手?

首先,创建帐户迁移-复数是广泛接受的

迁移必须包含所有重要字段

<?php
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateAccountsTable extends Migration
{
    public function up()
    {
        Schema::create('accounts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
       });
    }
    public function down()
    {
        Schema::drop('accounts');
    }
}

然后创建账户模型,

<?php namespace App;
use IlluminateAuthAuthenticatable;
use IlluminateDatabaseEloquentModel;
use IlluminateAuthPasswordsCanResetPassword;
use IlluminateFoundationAuthAccessAuthorizable;
use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
use IlluminateContractsAuthAccessAuthorizable as AuthorizableContract;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;
class Account extends Model implements AuthenticatableContract,
                                AuthorizableContract,
                                CanResetPasswordContract
{
     use Authenticatable, Authorizable, CanResetPassword;
     protected $table = 'accounts';
     protected $fillable = ['name', 'email', 'password'];
     protected $hidden = ['password', 'remember_token'];
}

转到configauth.php并更改此行:'model' => AppUser::class,'model' => AppAccount::class,

最新更新