【问题标题】:Laravel not creating migration table for existing modelLaravel 没有为现有模型创建迁移表
【发布时间】:2021-08-23 13:17:28
【问题描述】:

我使用这个命令生成了一个模型:

php artisan make:model Notification

模型文件如下所示:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Notification
 *
 * @property int $id
 * @property int|null $address_id
 * @property string $business_name
 * @property string $legal_structure
 * @property string|null $siret_number
 * @property string|null $rna_number
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @mixin \Eloquent
 */
class Notification extends Model
{
    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];
    
}

然后我使用 make migration 命令在数据库中创建模型的表,但它没有返回任何要迁移的内容,也没有创建任何表

【问题讨论】:

  • 不用单独执行命令,可以在制作模型时生成迁移,只需传递-m选项即可; php artisan make:model -m Notification。有关所有选项的列表,您可以检查 ModelMakeCommand 类上的 getOptions 方法。
  • 之后如何将模型属性添加到表格中?
  • 关于模型和迁移如何工作的一切都在官方Laravel documentation 中有很好的记录。文档范围从 Laravel v4.2 到 v8.x。我建议通读它以更好地理解框架。

标签: php laravel artisan-migrate


【解决方案1】:

如果您的应用程序使用 Laravel 的内置通知,那么您已经有一个 notifications 表和通过运行 php artisan notifications:table 创建的迁移。

如果表已经存在,Laravel 不会为该表创建另一个迁移文件。

您将需要使用不同的表名,我建议重命名您的通知模型以匹配您选择的任何表名。

【讨论】:

  • 我同意你的回答感谢@patricus先生的努力
猜你喜欢
  • 2017-09-15
  • 2023-01-06
  • 2018-05-22
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 2016-08-07
  • 2019-12-16
  • 2022-01-17
相关资源
最近更新 更多