Laravel 8 Tutorial - Application Migration

Published January 28, 2022

In this chapter, we are going to look at Laravel 8 Migration. Laravel migration simplifies the process of creating tables in your database without the need for a database manager, such as SQLite or PHPMyAdmin. In this post, we will go through the following topics:

  • How to Create a Laravel Migration

  • The Laravel Migration Structure

  • How to Create a Migration Connection

  • How to Run a Migration

1. How to create a Migration

We usually generate a new migration for Laravel 8 applications using the make: migration command:

php artisan make:migration create_flights_table

 

A new migration will be created in your database/migrations directory. Each laravel migration filename contains a unique timestamp, which will help Laravel to identify the migration sequence.

 

2. The Migration Structure

A migration class consists of two classes: an up class and a down class. The up class is used in the creation of new tables, the addition of additional columns, and the indexing of databases. On the other hand, the down approach reverses or modifies the operations performed by the up method.

Using these two methods, you will be able to use the Laravel schema builder to construct and update tables expressively.

 

Example

Here's an example of a migration that creates a "students" table

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class CreateStudentsTable extends Migration

{

    /**

     * Run the migrations.

     *

     * @return void

     */

    public function up()

    {

        Schema::create('students', function (Blueprint $table) {

            $table->id();

            $table->string('name');

            $table->string('course');

            $table->timestamps();

        });

    }

    /**

     * Reverse the migrations.

     *

     * @return void

     */

    public function down()

    {

        Schema::drop('students');

    }

}

 

 

3. How to Create a Migration Connection

Suppose you would like your migration to interact with another database rather than the default database connection. In that case, you need to set your connection in the $connection property of your migration.

Take a look at the example below. We are connecting our migration to another database connection known as RTSQL

/**

 * The database connection that the migration should use.

 *

 * @var string

 */

protected $connection = ' MySQL ';

 

/**

 * Run the migrations.

 *

 * @return void

 */

public function up()

{

    //

}

 

 

4. How to Run a Migration

Running your Laravel migrations is pretty easy. You only need to run the migrate Artisan command:

php artisan migrate

If you would like to know which migrations you have previously run, then you can use the following migrate: status Artisan command:

php artisan migrate: status

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

198 Views