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:
[SymfonyComponentDebugExceptionFatalErrorException] Can’t use method return value in write context
Solution tried:
- run
composer dump-autoload
(Fails) - Drop table in database, delete the migration file and restart again (works)
Previous migration file:
JavaScript
x
<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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:
JavaScript
<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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
Advertisement
Answer
Try this command it works for me
JavaScript
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.