How To Create Table Using Migration In Laravel 10

Websolutionstuff | Apr-21-2023 | Categories : Laravel MySQL

In this article, we will see how to create a table using migration in laravel 10. Migrations are like version control for your database, allowing your team to define and share the application's database schema definition.

If you have ever had to tell a teammate to manually add a column to their local database schema after pulling in your changes from source control, you've faced the problem that database migrations solve.

So, let's see laravel 10 create a table using migration, laravel 10 create migration and model, and how to migrate a specific table in laravel 10.

You may use the make:migration Artisan command to generate a database migration. The new migration will be placed in your database/migrations directory.

php artisan make:migration create_flights_table

Laravel will use the name of the migration to attempt to guess the name of the table and whether or not the migration will be creating a new table.

A migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should reverse the operations performed by the up method.

database/migrations

<?php
 
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::drop('flights');
    }
};

 

Run Migration:

You can run the migration using the following command.

php artisan migrate

 

Run Specific Migration:

Also, you can run specific migrations using the following command.

php artisan migrate --path=/database/migrations/2023_04_01_064006_create_flights_table.php

 

Migration Rollback:

To roll back the latest migration operation, you may use the rollback Artisan command. This command rolls back the last "batch" of migrations, which may include multiple migration files

php artisan migrate:rollback

You may roll back a limited number of migrations by providing the step option to the rollback command. For example, the following command will roll back the last five migrations:

php artisan migrate:rollback --step=5

 


You might also like:

Recommended Post
Featured Post
How to Disable Right Click using jQuery
How to Disable Right Click usi...

In this small post i will show you how to disable right click using jquery. Here, we will disable right click on pa...

Read More

Aug-18-2021

How to Install Elasticsearch in Laravel 10
How to Install Elasticsearch i...

Hey developers! If you're diving into the world of Laravel 10 and looking to supercharge your application's...

Read More

Dec-25-2023

How To Get Multiple Checkbox Value In React JS
How To Get Multiple Checkbox V...

In this article, we will see how to get multiple checkbox values in react js. In react, js click on the submit butt...

Read More

Aug-29-2022

How To Get Client IP Address In Laravel 10
How To Get Client IP Address I...

In this article, we will see how to get a client's IP address in laravel 10. Here we will learn about retrieving the...

Read More

Apr-07-2023