Laravel 11 Migrations: A Comprehensive Guide
What are Migrations?
Laravel migrations are a version control system for your database. They allow you to define your database schema (tables, columns, indexes, etc.) in PHP code, providing a structured and repeatable way to manage schema changes.
Why are Migrations Important?
- Version Control: Track database schema changes over time. Each change is recorded, allowing you to revert to previous versions.
- Reproducibility: Recreate the database consistently across different environments (development, testing, production). This eliminates discrepancies and ensures consistency.
- Reversibility: Roll back to previous database versions if necessary. This is essential for correcting errors or reverting to a previous state.
- Collaboration: Facilitate teamwork by codifying database changes. All team members can use the same migrations.
How Migrations Work in Laravel 11
-
Create Migrations: Use
php artisan make:migration migration_name
to generate migration files. These files contain theup()
anddown()
methods. -
Define Schema: Use the
Schema
class within theup()
method to define table structure. You can specify columns, data types, indexes, and constraints. -
Run Migrations: Execute
php artisan migrate
to apply migrations. Laravel will execute theup()
method of each migration in order. -
Rollback Migrations: Use
php artisan migrate:rollback
to revert the last migration orphp artisan migrate:reset
to revert all migrations. -
Modify Tables: Use
Schema::table()
to alter existing tables. You can add, modify, or delete columns.
Practical Examples
Creating a "users" Table
php artisan make:migration create_users_table --create=users
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('users');
}
}
Adding a "city" Column to the "users" Table
php artisan make:migration add_city_to_users_table --table=users
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddCityToUsersTable extends Migration
{
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('city')->nullable();
});
}
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('city');
});
}
}
Using make:model -m
The php artisan make:model ModelName -m
command creates both an Eloquent model and its associated migration, saving time and ensuring consistency. This is a very useful command for quickly creating models and their corresponding tables.
php artisan make:model Product -m
Laravel 11 Specifics
- Simplified Application Structure: Laravel 11 comes with a cleaner structure, making projects easier to manage.
-
Improved Test Performance with SQLite
:memory:
: Tests using SQLite are now faster, thanks to optimizations in Laravel 11. - Dedicated MariaDB Driver: Laravel 11 includes a dedicated driver for MariaDB, providing better integration and performance.
- Per-Second Rate Limiting: Laravel 11 introduces the ability to limit resource access rates per second, providing finer control.
Additional Tips
- Use descriptive migration names.
- Test migrations in your development environment before applying them to production.
- Combine migrations with seeders to populate tables with initial data.
- Consult the official Laravel documentation for detailed information.
For more details, consult the official Laravel 11 documentation.
Post a Comment
0Comments