Laravel | Migration Basics - GeeksforGeeks

30 Dec, 2019
In Laravel, Migration provides a way for easily sharing the schema of the database. It also makes the modification of the schema much easier. It is like creating a schema once and then sharing it many times. It gets very useful when you have multiple tables and columns as it would reduce the work over creating the tables manually.
To Create Migration: It can be created by using the artisan command as shown below:
php artisan make:migration create_articles_table
Here, articles are going to be the table and in place of that you can write any other table name which you want to create and is appropriate for the application but the table name as you saw, is to be in plural form. So, it is written as articles and not article. This is the naming scheme used by Laravel and it is important to specify create at the start and table at the end according to the naming scheme of a Migration file in Laravel.
All the migration file that we create using the artisan command are located at database/migrations directory. So, after we run the above command, it will generate a PHP file with the name we specified with the current date and time.
And the file will be created with some predefined class and functions as shown in the code below:
<?php  use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;  class CreateArticlesTable extends Migration{    public function up()    {        Schema::create('articles', function (Blueprint $table) {            $table->bigIncrements('id');            $table->timestamps();        });    }      public function down()    {        Schema::dropIfExists('articles');    }}
If you want to specify the name of the table different than the name that you specified as the file name then you can use an option as –create with the command as follows:
php artisan make:migration create_articles_table --create=gfg
With this command, the table name contained in the create() method will be gfg and not articles, which is specified in the name of the file.
<?php  use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;  class CreateArticlesTable extends Migration{    public function up()    {        Schema::create('gfg', function (Blueprint $table) {            $table->bigIncrements('id');            $table->timestamps();        });    }    public function down()    {        Schema::dropIfExists('gfg');    }}
Basic Structure of a Migration: A migration file contains a class with the name of the file specified while creating the migration and it extends Migration. In that we have two functions, the first one is up() function and the second one is down() function. The up() is called when we run the migration to create a table and columns specified and the ‘down()’ function is called when we want to undo the creation of ‘up()’ function.
In the up() function, we have create method of the Schema facade (schema builder) and as we say before, the first argument in this function is the name of the table to be created. The second argument is a function with a Blueprint object as a parameter and for defining the table.
In the down() function, we have a dropIfExists method of the schema builder which when called will drop the table.
To Run Migration: Before running a migration, we first have to create a MySQL Database and Connect to it. After that is done, to Run a Migration, we can use an Artisan command as follows:
php artisan migrate
This command will run the up() function in the migration class file and will create the table with the all columns specified.Note: This command will run the up() function and create all the tables in the database for all the migration file in the database/migrations directory.
To rollback any last migration which is done, we can use the following Artisan command:
php artisan migrate:rollback
This command will run the down() function in the migration class file.
Reference: https://laravel.com/docs/6.x/migrations
Laravel
PHP
Technical Scripter
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to fetch data from localserver database and display on HTML table using PHP ?
How to pass form variables from one page to other page in PHP ?
Create a drop-down list that options fetched from a MySQL database in PHP
How to create admin login page using PHP?
Different ways for passing data to view in Laravel
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?