Author - StudySection Post Views - 613 views
laravel certification

Laravel Migration

Migrations are generally used to modify the database schema. Along with Laravel’s schema builder, it builds the application’s database schema. Designing tables not only become easy with the use of migrations, but the entire table can also be created.

Migrations are stored in : database/migrations

  1. Generate Migration:
    Make:migration – This command is used to generate a migration

    php artisan make:migration sample_table

    This will generate a migration with the name SampleTable

  2. Migration Structure
    It contains two methods: up and down.
    New columns, tables or indexes are added to the database using the up method.
    The operations performed by the up method are reversed by the down method.

    <?php
    
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class SampleTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            //
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            //
        }
    }
    
  3. Running Migration
    php artisan migrate – this command is used to run the migration.
    This will create the migration and insert an entry into the migration table.

  4. RollBack a Migration
    php artisan migrate:rollback – This command is used to rollback a migration

  5. Drop All Tables & Migrate
    php artisan migrate:fresh – This command is used to drop all the tables in the database.

Tables

  1. Create Table
    Create method is used to create a new database table on the Schema facade. It accepts two arguments.

    • The name of the table
    • Blueprint object – Used to define the new table:

      Schema::create(‘sample_table’, function (Blueprint $table) {
      $table->increments(‘id’);
      });

  2. Rename a Table
    To rename an existing method use rename method

    Schema::rename($from, $to);

  3. Drop a Table
    To drop the table use drop or dropIfExists method.

    	
        Schema::drop('sample_table');
        or
        Schema::dropIfExists('sample_table');
    

Run a specific migration
We will create sample_table with some columns:

<?php

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

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

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
         Schema::dropIfExists('sample_table');
    }
}

To run only this migration we need to add the path of this migration in migrate command

php artisan migrate –path=/database/migrations/2019_04_08_121257_sample_table.php


With this, we can run a specific migration.

The table is created along with the fields.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri