Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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

like image 447
mushood badulla Avatar asked Feb 04 '17 13:02

mushood badulla


People also ask

What does php artisan migrate refresh do?

php artisan migrate:fresh is used when we want a fresh or new installation of our database. It deletes all the existing tables of the database and runs the migrate command. php artisan migrate:refresh is a two in one command that executes the :rollback command and the migrate command.

How do I move to Laravel without losing data?

Set your column details there, run the migrations using php artisan migrate and that's all. You'll have this new column in your users table without losing previously stored data.


2 Answers

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.

like image 179
NomanJaved Avatar answered Sep 28 '22 02:09

NomanJaved


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
like image 30
Prathamesh Doke Avatar answered Sep 28 '22 02:09

Prathamesh Doke