0

I’m trying to change a Laravel's default table name from its model name and plural to the other custom name. From reading around I thought I had to just update that in the app/Model/User.php for example then am done. But unfortunately its not working. Here is what I tried by changing the users table to the web_registration table:

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     *@var string[]
     **/
   

    

    /**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'web_registration';







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

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

But when I run the migrations I am still getting the users table instead of the web_registration table. Note that I have also run composer dump-autoload -o as suggested by other stack answers but still no any changes. Is there anywhere else where I need to change this?

Chanda chewe
  • 31
  • 1
  • 3

2 Answers2

0

You need to update your migrations.

With default laravel installation under the folder {your_app_root_folder}/database/migrations/ you will find your migrations. There should be migration called: CreateUsersTable - file will probably have a name something like: 2014_10_12_000000_create_users_table.php.

You will need to delete that migration in order to avoid having it migrated (created) when you are running php artisan migrate.

Then, you will need to create a new migration called CreateWebRegistrationTable through which you can create that table.

You can do it by running php artisan make:migration create_web_registration_table.

Then, run your migrations, and you will have web_registration table and no users table.

On your model, add protected $table = 'web_registration'; and you will be ready to go!

Ben
  • 2,060
  • 9
  • 21
0

If you want to change "users" table to "web_registration" table, you have to delete users migration in migrations directory and make a new migration named web_registration.

  • you wouldn't have to delete it, you can just rollback the migration and change that migration file and run it again – lagbox Jan 31 '22 at 16:51