简体   繁体   English

Laravel 5: php artisan migrate:refresh

[英]Laravel 5: php artisan migrate:refresh

I am working on a laravel project and each time I change my table (add or remove column) and run php artisan migrate:refresh .我正在处理一个 Laravel 项目,每次更改表(添加或删除列)并运行php artisan migrate:refresh I get this error:我收到此错误:

[Symfony\\Component\\Debug\\Exception\\FatalErrorException] Can't use method return value in write context [Symfony\\Component\\Debug\\Exception\\FatalErrorException] 无法在写入上下文中使用方法返回值

Solution tried:尝试的解决方案:

  1. run composer dump-autoload (Fails)运行composer dump-autoload (失败)
  2. Drop table in database, delete the migration file and restart again (works)删除数据库中的表,删除迁移文件并重新启动(有效)

Previous migration file:以前的迁移文件:

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

Changed migration file:更改迁移文件:

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

I added the user_id in the change file in the up function我在 up 函数的更改文件中添加了 user_id

Try this command it works for me 试试这个命令对我有用

php artisan migrate:fresh

However, be careful! 但是,要小心! This command will drop all data from your DB: 此命令将从数据库中删除所有数据:

Note: The migrate:fresh command will drop all tables from the database and then execute the migrate command. 注意:migrate:fresh命令将从数据库中删除所有表,然后执行migrate命令。

as per Laravel docs . 根据Laravel文档

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

Refresh the database and run all database seeds...刷新数据库并运行所有数据库种子...

php artisan migrate:refresh --seed

Read Docs阅读文档

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

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