Saturday 25 November 2017

Laravel Migrations Command Lines

I don't trust my memory well so I'll document some migrations command

php artisan make:migration add_paid_to_users --table="users"
This line will create a book table migrations

php artisan make:migration create_books_table --create="books"




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

class CreateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

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

This line will add a migration script to add author_id

php artisan make:migration add_author_id_to_book_table --table="books"


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

class AddAuthorIdToBookTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('books', function (Blueprint $table) {
            $table->integer('author_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('books', function (Blueprint $table) {
            //
        });
    }
}
My Template for creating migrations



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


class DifferentArtisanTypeForMySql extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::create('my_table1', function (Blueprint $table) {
            $table->increments('id');

            $table->string('string_col');
            $table->string('string_col_with_length', 500);


            //create unsigned field which is the same type for field with increment attribute
            $table->unsignedInteger('unsigned_int_index')->index();
            //create string token with unique index
            $table->string('token', 100)->unique();


            $table->enum('enum_field', ['Beauty', 'Business service', 'Childrens products'])->nullable();


            $table->timestamp('timestamp_field')->nullable();
            $table->date('date_field')->nullable();
            $table->boolean('boolean_field')->default(0);

            //will create integer field with 11 in length
            $table->integer('integer_field')->default(0);

            //will create integer field with 10 in length
            $table->integer('unsigned_integer_field')->unsigned();
            
            $table->decimal('decimal_field', 6, 1)->nullable()->default(0);
            $table->tinyInteger('tinyint_field');
            $table->smallInteger('smallinteger_field')->nullable();

            $table->char('char_field', 2)->default('');
            $table->text('text_field');


        });



       // DB::statement('ALTER TABLE campaign_influencer_posts CHANGE COLUMN social_id account_id INT(11) NOT NULL ;');



        // Create table non auto increment field with index field
        Schema::create('my_table2', function ($table) {
            $table->integer('id')->unsigned()->index();
            $table->string('capital', 255)->nullable();
            $table->string('citizenship', 255)->nullable();

            $table->primary('id');
        });


        //Do you want to add a new records right away?
        Schema::create('my_table3', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->tinyInteger('active')->index();
        });

        DB::table('my_table3')->insert([
            'name' => 'Instagram',
            'active' => 1
        ]);

        DB::table('my_table3')->insert([
            'name' => 'Facebook',
            'active' => 1
        ]);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

        Schema::table('my_table1', function (Blueprint $table) {
            $table->dropColumn('string_col');
        });

       // DB::statement('ALTER TABLE campaign_influencer_posts CHANGE COLUMN account_id social_id INT(11) NOT NULL ;');


        Schema::dropIfExists('my_table1');
        Schema::dropIfExists('my_table2');
        Schema::dropIfExists('my_table3');    
    }

    
    //You might wish to rename some fields
    // but for laravel 5.X 
    // you need to run composer require doctrine/dbal

    public function up()
    {
        Schema::table('demographics', function (Blueprint $table) {
            $table->renameColumn('social_id', 'account_id');
        });

    }
   
}



Template for migration with foreign key


    public function up(): void
    {
        Schema::table('child_table', function (Blueprint $table) {
            $table->integer('foreign_column', false, true)->nullable()->after('column_name');
            $table->foreign('foreign_column')->references('id')->on('parent_table');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('child_table', function (Blueprint $table) {
            $table->dropForeign('foreign_key_here');
            $table->dropColumn('foreign_column');
        });
    }
Here are the list of data type columns that can be added https://laravel.com/docs/5.5/migrations#creating-columns
Share:

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. add these in the rollback when updating a column
    $table->dropColumn('column_name');

    add these in rollback when creating a new table
    Schema::dropIfExists('table_name');

    ReplyDelete
  3. when you do migration on heroku run this command

    heroku run php artisan migrate

    ReplyDelete
  4. To Rollback migration

    php artisan migrate:rollback --step=1

    ReplyDelete
  5. if enum is not working add these lines in the migration class

    public function __construct() {
    \Illuminate\Support\Facades\DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
    }


    public function up()
    {

    Schema::table('brands', function (Blueprint $table) {
    $table->dropForeign('brands_user_id_foreign');
    $table->dropColumn('id');
    $table->dropColumn('user_id');
    });


    Schema::table('brands', function (Blueprint $table) {
    $table->increments('id')->before('name');
    $table->string('email')->nullable();
    $table->string('cover_photo_url')->nullable();
    $table->enum('status', ['Active', 'Archived'])->after('name');
    });


    Schema::table('campaigns', function (Blueprint $table) {
    $table->renameColumn('brand_user_id', 'brand_id');
    });

    }

    ReplyDelete
  6. add this if if having trouble with enum
    public function __construct() {
    \Illuminate\Support\Facades\DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
    }

    public function up()
    {
    }

    public function down()
    {
    Schema::table('brands', function (Blueprint $table) {

    });
    }

    ReplyDelete
  7. To migrate all files inside migrations folder

    php artisan migrate:fresh --path=/database/migrations/*

    ReplyDelete

Popular Posts

Recent Posts

Pages

Powered by Blogger.

About Me

My photo
For the past 10 years, I've been playing with codes using PHP, Java, Rails. I do this for a living and love new things to learn and the challenges that comes with it. Besides programming I love spending time with friends and family and can often be found together catching the latest movie or planning a trip to someplace I've never been before.