Hey fellow developers! 🚀 If you're stepping into the Laravel universe and find yourself wondering about this "migration" thing, fear not – we're about to embark on a journey together. In this step-by-step guide, I'll walk you through the process of creating migrations in Laravel 10.
In this article, we'll see how to create migration in laravel 10, laravel 10 creates a migration, laravel migration command, run specific migration laravel 8/9/10, and create migration in laravel command.
Think of migrations as your trusty sidekick in the world of databases. They help you define and manage your database schema effortlessly. Whether you're adding a new table, tweaking a column, or undoing a change – migrations have got your back.
No need to be a database guru; I've crafted this guide with simplicity in mind. By the end of it, you'll be confidently sculpting your database structure like a Laravel maestro.
Let's dive into the wonderful world of migrations – one step at a time! 💻✨
If you haven't installed Laravel yet, use the following command to create a new Laravel project
composer create-project laravel/laravel laravel-migration-example
Open the .env
file in your Laravel project directory and set up your database connection details, such as database name, username, and password.
To create a migration, use the Artisan command:
php artisan make:migration create_example_table
This command will generate a new migration file in the database/migrations
directory.
Create Migration with Table:
php artisan make:migration create_examples_table --table=examples
Open the created migration file (located in database/migrations
). The file name will be something like 2023_01_01_000000_create_example_table.php
. Add the necessary columns and any other configurations you need inside the up
method.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateExampleTable extends Migration
{
public function up()
{
Schema::create('example', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('example');
}
}
In this example, we're creating a table named 'example' with columns for 'id', 'name', 'description', and timestamps.
Execute the migration to apply the changes to the database:
php artisan migrate
This command will create the 'example' table in your configured database.
Run Specific Migration
php artisan migrate --path=/database/migrations/2023_01_01_000000_create_example_table.php
If you need to rollback the last database migration, you can use the following command:
php artisan migrate:rollback
This is useful if you want to undo the last set of changes to your database.
You might also like: