Laravel 5:php artisan migrate:刷新

时间:2017-02-04 13:37:33

标签: php laravel-5 laravel-5.5

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

  

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

尝试解决方案:

  1. 运行composer dump-autoload(失败)
  2. 删除数据库中的表,删除迁移文件并重新启动(正常工作)
  3. 上一个迁移文件:

    <?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文件中添加了user_id

2 个答案:

答案 0 :(得分:3)

尝试此命令对我有用

php artisan migrate:fresh

答案 1 :(得分:0)

  

尝试一下   触发此命令

php artisan make:migration add_user_id_to_comments_table --table=comments
  

然后将创建一个新的迁移文件

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

然后使用

php artisan migrate