Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Laravel's migration up and down method work?

In this code the table is created in the up method and deleted in the down() method. When I run the migration, the table is created but not deleted. In what way can I trigger the down method, so that I get a better understanding on how the two methods work?

<?php

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

class CreateFlightsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }
}
like image 252
Deeven Avatar asked Mar 01 '18 18:03

Deeven


People also ask

How do you run down migration?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.

How does Laravel migration work?

Laravel will use the name of the migration to attempt to guess the name of the table and whether or not the migration will be creating a new table. If Laravel is able to determine the table name from the migration name, Laravel will pre-fill the generated migration file with the specified table.

What do you mean by database migration how it is done in Laravel explain with the help of an example?

Laravel Migration is an essential feature in Laravel that allows you to create a table in your database. It allows you to modify and share the application's database schema. You can modify the table by adding a new column or deleting an existing column.

What are php migrations?

Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state. Migrations are typically paired with the Schema Builder to easily manage your application's schema.


1 Answers

In your example:

php artisan migrate will run your up() method.

php artisan migrate:rollback will run your down() method.

Read up on the excellent docs: https://laravel.com/docs/5.7/migrations#migration-structure

like image 105
mikemols Avatar answered Nov 14 '22 22:11

mikemols