【问题标题】:Method Illuminate\Database\Schema\Blueprint::comment does not exist方法 Illuminate\\Database\\Schema\\Blueprint::comment 不存在
【发布时间】:2022-12-21 23:05:15
【问题描述】:

当我使用以下代码时,我需要一个简单问题的帮助

DB::statement("ALTER TABLE user comment 'User comment'");

此代码在我的 laravel 迁移中有效。

但是当我使用相同的代码时 laravel 文档

Schema::create('user', function (Blueprint $table) { $table->comment('User comment'); });

我在终端的输出如下

BadMethodCallException异常

方法 Illuminate\Database\Schema\Blueprint::comment 不存在。

我试过

Schema::create('user', function (Blueprint $table) { $table->comment('User comment'); });

我预期的结果是成功的

【问题讨论】:

    标签: php laravel migration lumen


    【解决方案1】:

    在使用 comment() 之前,您应该先定义列。由于 comment 是来自 IlluminateDatabaseSchemaColumnDefinition::class 的方法,因此您无法使用 $table 访问它,它是 IlluminateDatabaseSchemaBlueprint::class 的一个实例:

    $table->string('name'); // Return type is ColumnDefinition class it means the you can access the comment() after the string(),
    
    $table->string('name')->comment('Name of the user');
    

    Laravel - Column Modifiers

    【讨论】:

      【解决方案2】:

      MySQL 和 Postgres 的迁移表 cmets 随 laravel 9.14 版本发布:https://laravel-news.com/laravel-9-14-0

      所以,如果你想使用这个功能,你必须更新你的 laravel 版本。

      【讨论】:

        猜你喜欢
        • 2021-04-12
        • 1970-01-01
        • 2021-12-30
        • 2014-12-11
        • 1970-01-01
        • 1970-01-01
        • 2020-11-21
        • 2018-09-24
        • 2021-09-08
        相关资源
        最近更新 更多