Artisan command not to lose data from database

Asked

Viewed 1,061 times

2

Hello I am starting to work with Laravel 5 and I am having a problem when I run the migrate commands the data is lost in the database, this is expected?

the command php artisan migrate works on the first call and the others php artisan refresh and php artisan reset causes database data to be lost, there is some specific command for it?

1 answer

1

If you make changes like adding/deleting a column from some table it is recommended to create a new Migration similar to this one

<?php 

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddColumnOwnerToLikes extends Migration
{
    /**
      * Run the migrations.
      *
      * @return void
    */
    public function up()
    {
        Schema::table('likes' , function(Blueprint $table){
        $table->integer('user_id');
    });

}

    /**
      * Reverse the migrations.
      *
      * @return void
    */
    public function down()
    {
     //
    }

}

after creating Migration run the command:

php artisan migrate

soon after the execution your new column will be created or deleted.

With the commands:

php artisan migrate:refresh
php artisan migrate:reset

data will be deleted as all tables will be deleted and recreated

  • got it... the strange thing is that in the course of an application these changes and Migrations can become uncontrollable ie, many Migrations for a table, which on top of that will have data. understands

  • You will have full control over your Migrations, just create when you need to change the structure of the tables and everything will be as before, just be careful not to make drastic edits that can prevent the operation of the application

Browser other questions tagged

You are not signed in. Login or sign up in order to post.