0

我正在尝试将 Laravel 的默认表名从其模型名称和复数更改为另一个自定义名称。通过阅读,我认为我必须在 app/Model/User.php 中更新它,例如然后完成。但不幸的是它不起作用。这是我通过将 users 表更改为 web_registration 表的尝试:

<?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',
    ];
}

但是当我运行迁移时,我仍然得到用户表而不是 web_registration 表。请注意,我还按照其他堆栈答案的建议运行了 composer dump-autoload -o ,但仍然没有任何更改。还有其他地方我需要更改吗?

4

2 回答 2

0

您需要更新迁移。

使用文件夹下的默认 laravel 安装,{your_app_root_folder}/database/migrations/您将找到您的迁移。应该CreateUsersTable有一个名为的迁移: - 文件的名称可能类似于:2014_10_12_000000_create_users_table.php.

您需要删除该迁移以避免在运行时迁移(创建)它php artisan migrate

然后,您将需要创建一个名为的新迁移CreateWebRegistrationTable,您可以通过它创建该表。

你可以通过运行来做到这一点php artisan make:migration create_web_registration_table

然后,运行您的迁移,您将有web_registration表和没有users表。

在您的模型上,添加protected $table = 'web_registration';,您就可以开始了!

于 2022-01-31T13:42:40.577 回答
0

如果要将“users”表更改为“web_registration”表,则必须在迁移目录中删除用户迁移并创建一个名为 web_registration 的新迁移。

于 2022-01-31T13:44:08.903 回答