In this article, we will see laravel 10 create a table using migration. 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.
In this article, we'll see how to create a new table using laravel 10 migration. Also, you can rollback a migration using the command 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');
}
};
You can run the migration using the following command.
php artisan migrate
Also, you can run specific migrations using the following command.
php artisan migrate --path=/database/migrations/2023_04_01_064006_create_flights_table.php
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:
- Read Also: Change Column Name and Type in Migration in Laravel 10
- Read Also: Select2 Autocomplete Search Using Ajax in Laravel 10
- Read Also: Laravel 10 User Roles and Permissions Tutorial
- Read Also: How to Validate Form in Laravel 10