Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Laravel auth table name and column names

I want to change the table name and some column names of laravel auth table.

  • Change table name from 'users' to 'accounts'
  • Change table column name from 'name' to 'username'
  • Change table column name from 'email' to 'email_addr'
  • Change table column name from 'updated_at' to 'last_updated_at'

What steps do I take or which code do I edit without breaking something?

I tried this before, and registration worked, but login didn't. Whenever I tried to log in, I got redirected back to the login page.

like image 958
Savvy Sage Avatar asked Apr 11 '19 06:04

Savvy Sage


2 Answers

You can follow the below given steps:

  1. Create/modify migration to change the users table to accounts
  2. Create migration to change the column name as per your requirements for table accounts. Make sure this model call is extends Authenticatable
  3. Create model class for accounts table.
  4. Make sure to add fillable and hidden attributes of table.
  5. Now check the login.blade.php file and change the email input text field name to email_address.

With the above all steps, we are ready with View part now let's start with customising the Auth

  1. Now open config/auth.php

    • Change from 'model' => App\User::class, to 'model' => App\Account:class inside providers array.
  2. Now we need to add new function inside app/Http/Auth/LoginController.php like below:

public function username(){ return 'email_address'; // this string is column of accounts table which we are going use for login }

Now we are done with all adjustment, you can test the functionality.

I have tested the functionality and its working like charm :)

like image 153
Pintu Kumar Avatar answered Sep 28 '22 20:09

Pintu Kumar


you have to extend "Illuminate\Foundation\Auth\User" in your Account model.

namespace App;

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

class Account extends Authenticatable
{ 
  use Notifiable;

  //code here
  public function getEmailAttribute() {
      return $this->email_addr;
  }

  public function setEmailAttribute($value)
  {
    $this->attributes['email_addr'] = strtolower($value);
  }
}

and change in configuration file in "config/auth.php" in providers array

'users' => [
        'driver' => 'eloquent',
        'model' => App\Account::class,  //replace User to Account
    ],
like image 43
Surender Singh Rawat Avatar answered Sep 28 '22 19:09

Surender Singh Rawat