Laravel 5:PHP Artisan迁移:刷新

mus*_*lla 3 php laravel-5 laravel-5.5

我正在做一个laravel项目,每次更改表(添加或删除列)并运行php artisan migrate:refresh。我收到此错误:

[Symfony \ Component \ Debug \ Exception \ FatalErrorException]无法在写上下文中使用方法返回值

解决方案尝试:

  1. 运行composer dump-autoload(失败)
  2. 将表拖放到数据库中,删除迁移文件,然后再次重新启动(有效)

先前的迁移文件:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('post_id');
            $table->string('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('comments');
    }
}
Run Code Online (Sandbox Code Playgroud)

更改的迁移文件:

    <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('post_id');
            $table->string('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('comments');
    }
}
Run Code Online (Sandbox Code Playgroud)

我在up函数的更改文件中添加了user_id

Nom*_*ved 5

试试这个命令对我有用

php artisan migrate:fresh
Run Code Online (Sandbox Code Playgroud)

但是,要小心!此命令将从数据库中删除所有数据:

注意:migrate:fresh命令将从数据库中删除所有表,然后执行migrate命令。

根据Laravel文档