Laravel

How To Create A Table Using Migration In Laravel?

Hello guys, I know the people who just started learning Laravel are very much excited to learn table migration in Laravel, Today I will show you how we can easily migrate our tables to the database.

While creating a database using command and migrate it to our table is very easy just follow the below step. You will reach your destiny.

migration  in laravel

Create Migration

Use the below command to create the migration table

php artisan make:migration create_userprofiles_table

After running this command you can see that a migration file is created under database->migration with named as 2019_01_15_112359_create_userprofiles_table.php

Open that file you file have below code

<?php

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

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

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

Now we will add some columns into the table

<?php

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

class CreateUserprofilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('userprofiles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('email')->nullable();
            $table->string('location')->nullable();
            $table->string('city')->nullable();
            $table->string('house_no')->nullable();
            $table->string('owner_name')->nullable();
            $table->bigInteger('price')->nullable();
            $table->string('image')->nullable();
            $table->timestamps();
        });
    }

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

Now in your command panel write php artisan:migrate you can see the below tables are created in your database

migrated-table
migrated table

If you want to migrate only specific table then you can use the below command

 php artisan migrate --path=/database/migrations/2019_01_15_112359_create_userprofiles_table.php

Want to delete your created table then just run the below command

 php artisan migrate:rollback --step=1

Need depth knowledge of Laravel migration then follow the link https://laravel.com/docs/7.x/migrations

You can also read our post regarding Laravel from here https://blog.codehunger.in/category/laravel/

I hope this post will help you understand the migration in laravel still having confusion feel free to comment, We are here to help you.

Related Articles

Leave a Reply

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

Back to top button