Laravel


Database Migrations 5.0–5.3

1.0
2.0
3.0
3.1
3.2
4.0
4.1
4.2
5.0
5.1 (LTS)
5.2
5.3

This draft deletes the entire topic.

Introduction

Introduction

expand all collapse all

Examples

  • 8

    Each migration should have an up() method and a down() method. The purpose of the up() method is to perform the required operations to put the database schema in its new state, and the purpose of the down() method is to reverse any operations performed by the up() method. Ensuring that the down() method correctly reverses your operations is critical to being able to rollback database schema changes.

    An example migration file may look like this:

    <?php
    
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class AddLastLoggedInToUsersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('users', function (Blueprint $table) {
                $table->dateTime('last_logged_in')->nullable();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('users', function (Blueprint $table) {
                $table->dropColumn('last_logged_in');
            });
        }
    }
    

    When running this migration, Laravel will generate the following SQL to run against your database:

    ALTER TABLE `users` ADD `last_logged_in` DATETIME NULL
    
  • 4

    To control your database in Laravel is by using migrations. Create migration with artisan:

    php artisan make:migration create_first_table --create=first_table
    

    This will generate the class CreateFirstTable. Inside the up method you can create your columns:

    <?php
    
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateFirstTable extends Migration
    {
        public function up()
        {
            Schema::create('first_table', function (Blueprint $table) {
                $table->increments('id');
                $table->string('first_string_column_name');
                $table->integer('secont_integer_column_name');
                $table->timestamps();
            });
        }
    
        public function down()
        {
            Schema::drop('first_table');
        }
    } 
    

    At the end to run all of your migrations classes you can run the artisan command:

    php artisan migrate
    

    This will create your tables and your columns in your database. Other useful migrate command are:

    • php artisan migrate:rollback - Rollback the last database migration
    • php artisan migrate:reset - Rollback all database migrations
    • php artisan migrate:refresh - Reset and re-run all migrations
    • php artisan migrate:status - Show the status of each migration
  • 2

    Creating a new migration file with the correct filename every time you need to change your schema would be a chore. Thankfully, Laravel's artisan command can generate the migration for you:

    php artisan make:migration add_last_logged_in_to_users_table
    

    You can also use the --table and --create flags with the above command. These are optional and just for convenience, and will insert the relevant boilerplate code into the migration file.

    php artisan make:migration add_last_logged_in_to_users_table --table=users
    
    php artisan make:migration create_logs_table --create=logs
    

    You can specify a custom output path for the generated migration using the --path option. The path is relative to the application's base path.

    php artisan make:migration --path=app/Modules/User/Migrations
    
Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about Database Migrations? Ask Question

Topic Outline