运行“php artisan migrate ”报“Method Illuminate\Database\Schema\Blueprint::comment does not exist.”错误
以下是解决方法:
<?php
namespace App\Support\Database;
use Exception;
use Illuminate\Support\Fluent;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Grammars\Grammar;
use Illuminate\Database\Migrations\Migration as BaseMigration;
class Migration extends BaseMigration
{
public function __construct()
{
$this->addCommentTableMethod();
}
protected function addCommentTableMethod()
{
Blueprint::macro('comment', function ($comment) {
if (!Grammar::hasMacro('compileCommentTable')) {
Grammar::macro('compileCommentTable', function (Blueprint $blueprint, Fluent $command, Connection $connection) {
switch ($database_driver = $connection->getDriverName()) {
case 'mysql':
return 'alter table ' . $this->wrapTable($blueprint) . $this->modifyComment($blueprint, $command);
case 'pgsql':
return sprintf(
'comment on table %s is %s',
$this->wrapTable($blueprint),
"'" . str_replace("'", "''", $command->comment) . "'"
);
case 'sqlserver':
case 'sqlite':
default:
throw new Exception("The {$database_driver} not support table comment.");
}
});
}
return $this->addCommand('commentTable', compact('comment'));
});
}
}
调用此类,再执行命令
<?php
use App\Support\Database\Migration;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
class CreateAdminTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('admin', function (Blueprint $table) {
$table->id();
$table->string('name', 30)->comment('名称');
$table->string('email',10)->comment('邮箱');
$table->comment('管理表');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('admin');
}
}