Migrations in laravel

in #utopian-io8 years ago (edited)

1_qBVXqyNcooX4Saoa62sJSA.jpeg

image-source

This tutorial we will learn one of the features in laravel named migrations, migrations will make automatically when we want to create a table. the advantages of migrations for example there are some people who work on the same project, they do not need to export and import databases or create manual database, so when they want to take laravel project, they just run the class or method of migrations through command line or terminal, and automatically in its database will make what we want.

What is migrations ?

Migrations is one of the interesting features in laravel, with this feature when we work with teams for a project. we do not need to import or export the database or make it manually, we just need to run the command line, and automatically will be imported. and its advantages again with migrations, migrations like version control of our database. because we can undo what we have done.

Create schema migrations

Open command prompt in your laravel project. and then type this one
php artisan make:migration table_category --create=categories
Screenshot_24.png

php artisan make:migration : This is the laravel's command to make the migration.
table_category : This is the class name that will be created in migrations.
--create=categories : This is the name table that will be created in the database

Screenshot_25.png
2017_12_31_132818_table_category.php

If there is no problem then migrations class successfully created. Automatically laravel will create a class, in the class there are 2 functions, up () and down ().
up () : functions to create a database schema.
down () :function to delete the table in this tutorial table name is "categories".
class TableCategory extends Migration{ } : This is a function name that automatically becomes camelcase .

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class TableCategory extends Migration
{
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('desc');
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::dropIfExists('categories');
    }
}

Laravel will create 2 columns automatically , id and timestamp. I will add some columns in schema database like this.
public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('desc');
            $table->timestamps();
        });
    }

I added two columns . $table->string('name') ; and $table->string('desc') ; . now we do my migration in our database

Create migrations

We have created a migration schema. and add two columns, now we have to make the migration. Open the command prompt.
php artisan migrate

Screenshot_26.png
We have run the migration command, we can see the results in our mysql. di tutorial ini saya telah membuat databas dengan nama Test.
Screenshot_22.png
We can see the table category has been created automatically, and then we can see the structure of table category .
Screenshot_23.png

We have successfully migrated, a feature that is very useful for us if working with a team, we do not have to bother to share the database with others. so many of me hopefully this tutorial useful for you, thank you



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Congratulations @alfarisi94! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of posts published

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @alfarisi94 I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.080
BTC 62814.07
ETH 1666.58
USDT 1.00
SBD 0.42