Dom
Dom

Reputation: 878

Changing the name of the "users" table to account Laravel 5

Fairly new to Laravel and would like to follow the table conventions I'm used to.

The default table name for accounts is "users" and I'd like to change it to "account." If anything, I'd like to change it to "user" and remove the plural.

I've already used migrate to make a cloned table of users called "account" and I'm just trying to figure out what all I have to do to existing code to make it work for logging in

It looks like I'll have to somehow update "app/Http/Auth/AuthController.php", but I'm not quit sure what it is I'll have to do...

Do I need to:

I guess another option is just scrapping their AuthController and establishing my own and just calling a new object of Account... Is this the route I should take?

Upvotes: 0

Views: 1734

Answers (2)

Emeka Mbah
Emeka Mbah

Reputation: 17520

Firstly, create account(s) migration - plural is widely acceptable

Migration must contain all important fields

<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
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');
    }
}

Then create Account model,

<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword 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'];
}

head over to config\auth.php and change this line: 'model' => App\User::class, to 'model' => App\Account::class,

Upvotes: 3

Luceos
Luceos

Reputation: 6720

I would simply extend the User class and overrule some things if you want to have the Model named Account:

Edit the table property in the Account class, see: https://github.com/laravel/laravel/blob/master/app/User.php#L24

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

Once your class Account is created edit the configured authentication class, see: https://github.com/laravel/laravel/blob/master/config/auth.php#L31

If you only want to overrule the table used by User, edit the User class:

protected $table = 'accounts';

To be honest, why bother? Taylor provided this skeleton for you to kickstart your application, why not use that especially if you're new to Laravel?

Upvotes: 4

Related Questions