3

I have a working Laravel app, I needed to update a Model and add a new column to the database. In order to do this I added the following code to the database/migrations/table.php file:

Schema::table('client', function (Blueprint $table) {
        $table->string('newColumn');
});

When I try to update the database (with php artisan migrate) I get the message "Nothing to migrate". I noticed that the only way to apply this change is doing php artisan migrate:refresh or reset, but both commands drop every line on the database.

Is there anyway to update my database columns without dropping everything?

Burgi
  • 421
  • 8
  • 24
DevOti
  • 133
  • 1
  • 1
  • 11
  • 1
    Do you have a migrations table? And did you create your migration using `php artisan make:migration add_newColumn_to_client`? – The Maniac May 23 '16 at 16:32
  • no, i created the the database migration table using make command but not to add the column, so thats the problem. i added the new field on the migrations file. thanks! – DevOti May 23 '16 at 16:36
  • 1
    Right on, glad it was that easy! – The Maniac May 23 '16 at 16:44

2 Answers2

3

The complete solution The Maniac described would be:

  • Create the new column with:php artisan make:migration add_newColumn_to_client.
  • Add the following code to the newly created file (date_add_newColumn_to_client):

    public function up()
    {
        Schema::table('client', function (Blueprint $table) {
            $table->string('newColumn');
        });
    }
    
  • run php artisan migrate

noufalcep
  • 3,446
  • 15
  • 33
  • 51
DevOti
  • 133
  • 1
  • 1
  • 11
0

If you'll only add some column[s], you can export data with complete inserts statements, run migrate:refresh and re-import the data. For MySQL:

mysqldump -u {USER} -p {DATABASE} --no-create-db --no-create-info --complete-insert > yourdata.sql
artisan migrate:refresh
mysql -u {USER} -p {DATABASE} < yourdata.sql
juebel
  • 1