Laravel 5: php artisan migrate:refresh

Laravel 5: php artisan migrate:refresh

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

[Symfony\Component\Debug\Exception\FatalErrorException] Can't use method return value in write context

尝试过的解决方案:

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

上一个迁移文件:

<?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');
    }
}

已更改迁移文件:

    <?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');
    }
}

我在up函数的change file中添加了user_id

试试这个对我有用的命令

php artisan migrate:fresh

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

Note: The migrate:fresh command will drop all tables from the database and then execute the migrate command.

as per Laravel docs.

try this fire this command

php artisan make:migration add_user_id_to_comments_table --table=comments

this will create a new migration file then

$table->integer('user_id')->after('id');

then use

php artisan migrate

刷新数据库并运行所有数据库种子...

php artisan migrate:fresh --seed

阅读Docs