如何在Laravel中更改默认通知表名称

Maj*_*Afy 3 php notifications laravel

我想更改notification表名称,所以我更改了迁移文件:

php artisan notification:table

Schema::create('member_notifications', function (Blueprint $table) {
      $table->uuid('id')->primary();
      $table->string('type');
      $table->morphs('notifiable');
      $table->text('data');
      $table->timestamp('read_at')->nullable();
      $table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)

并尝试$tableNotification课堂上使用:

protected $table = 'member_notifications';
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试在数据库中存储通知时,出现此错误:

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "notifications" does not exist
Run Code Online (Sandbox Code Playgroud)

Art*_*oda 6

如果要将默认通知表更改为自定义表,则应做以下三件事:

  1. 将迁移文件中的表名称更改为member_notifications。运行迁移。
  2. 例如,创建模型DatabaseMemberNotification。从中扩展Illuminate\Notifications\DatabaseNotification并覆盖属性

    protected $table = 'member_notifications';
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在默认User模型中,添加以下功能:

    public function notifications()
    {
        return $this->morphMany(DatabaseMemberNotification::class, 'notifiable')->orderBy('created_at', 'desc');
    }
    
    Run Code Online (Sandbox Code Playgroud)

    实际上它将覆盖特征中的notifications()方法 HasDatabaseNotifications

    注意:第一个参数应该是您的新模型类。