【问题标题】: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