Home >>Laravel Tutorial >Laravel Migrations

Laravel Migrations

Laravel Migrations

Creation of table in database in laravel is an easy task with laravel. Database schema can be easily modified and shared. Modification can be addition of new column or deleltion of existing columns.

Laravel migrations can add new column or delete records in DB without deleting actual records. This is helpful in projects having different teams working on different modules. No need to pass on the SQL file to the different systems. It act like version control of the database.

Make Migratoins

To make migrations

make:migrations

command is used. The general syntax of making migrations is:

php artisan make:migration message_you_want_to_remember

After running this command new migrations are created inside database/migrations directory and to determine the timing of migrations timestamp is included in filename. --table and --create options can also be used to indicate the name of the table and whether the migration will be creating a new table which will fill up the specified field in genrated migrations.

php artisan make:migration create_blogs_table --create=blogs
//php artisan make:migration add_votes_to_blogs_table --table=blogs

Laravel Migration

custom ouput path can also be specified by using --path after make:migrations along with path to set own path for migrations. It should be relative to base path of project of laravel.

Migration Structure

Migrations includes two methods inside the file: up and down. Up method helps to add the new updates to the database i.e. creating new tables or coulmns whereas down reverse the operation performed by up. To create and modify tables laravel schema builder is used, below shown the structure of a migration file:

<?php
use Illuminate\Support\Facades\Schema;  
use Illuminate\Database\Schema\Blueprint;  
use Illuminate\Database\Migrations\Migration;  
  
class CreateBlogsTable extends Migration  
{  
    /** 
     * Run the migrations. 
     * 
     * @return void 
     */  
    public function up()  
    {  
            Schema::create('blogs', function (Blueprint $table) {  
            $table->bigIncrements('id');  
            $table->string('title');  
            $table->string('desciption');  
            $table->rememberToken();  
            $table->timestamps();  
        });  
    }  
  
    /** 
     * Reverse the migrations. 
     * 
     * @return void 
     */  
    public function down()  
    {  
        Schema::dropIfExists('blogs');  
    }  
}
?>

Migrations can be run by the command

php artisan make:migraion

For some destructive migrations --force flag is used to run without prompt of confirmation like so:

php artisan migrate --force

Laravel Migration

After Doing Migration Check Your database Blogs Table created


No Sidebar ads