繁体   English   中英

Laravel 5: php artisan migrate:refresh

[英]Laravel 5: php artisan migrate:refresh

我正在处理一个 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');
    }
}

更改迁移文件:

    <?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 函数的更改文件中添加了 user_id

试试这个命令对我有用

php artisan migrate:fresh

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

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

根据Laravel文档

试试这个命令

php artisan make:migration add_user_id_to_comments_table --table=comments

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

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

然后使用

php artisan migrate

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

php artisan migrate:refresh --seed

阅读文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM