php - How to use laravel migrations

769

I am using Laravel framework of php for development. I want to use migration for table creation-manipulation. These are the steps I took:

  1. I create migration with commandphp artisan migrate:make create_users_table, which create a migration file and in its up function, I wrote down my schema and then I ran it and it got successfully executed.

  2. After that, I tried again run the same migration and as a result, its shows the error that, 'table exist'.

  3. Then I tried to use rollback function but it give error 'nothing to roll back' .

    So, how to rollback that migration or to execute down function of migration. Also, when I created new migration and in migration file's up function, I wrote code for dropping table created by my earlier migration and I execute with commandphp artisan migrate , by this all migration got executed (also my earlier one) and shows me the error, 'table already exist' (obvious).

So, now I'm stuck, is there a function to execute a special/particular migration? How do I do this?

560

Answer

Solution:

When you create a migration withartisan migrate:make you should write theup anddown methods. Thedown method should do the OPPOSITE of what theup method does.

public function up()
{
    Schema::create('users', function($table)
    {
        $table->increments('id');
        $table->string('username');
    });
}

public function down()
{
    // We DROP the table because we created it in the "up" method.
    Schema::drop('users');
}

It sounds to me like you have some code in yourup method that doesn't belong there, although this is hard to say without seeing your code. I suggest you clear our themigrations table (there may or may not be any records). You'll also need to manually drop the table you created through the migration. Then you can start fresh.

Remember you can also usedropIfExists to drop a table only if it exists.

public function down()
{
    // Drop the table only if it exists.
    Schema::dropIfExists('users');
}
653

Answer

Solution:

when you create 1 migration in laravel, each of migration include 2 method: 'up()' and 'down()'.

+) up() method will be excuted when you run command 'php artisan migrate'.

+) down() method will be excuted when you quan to come back the status before you run command "php artisan migrate".

you can try excuting command: 'php artisan migrate:refresh' to rollback all of migration and after excute command 'php artisan migrate' auto.

People are also looking for solutions to the problem: php - Best way to store app settings that are configurable from administrator interface?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.