10

I am working on a laravel project and each time I change my table (add or remove column) and run php artisan migrate:refresh. I get this error:

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

Solution tried:

  1. run composer dump-autoload (Fails)
  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

mushood badulla
  • 1,043
  • 1
  • 12
  • 19

3 Answers3

19

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.

as per Laravel docs 9.x.

NomanJaved
  • 1,324
  • 17
  • 32
1

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
Prathamesh Doke
  • 797
  • 2
  • 16
  • 38
-1

Refresh the database and run all database seeds...

php artisan migrate:fresh --seed

Read Docs

AloneCoder
  • 1
  • 1
  • 4