Introduction
Laravel migrations provide a powerful way to manage your database schema. This guide will cover how to create migrations, change column names, modify column types, remove columns, and handle indexing and foreign keys with detailed examples.
1. Creating a Migration
To create a migration, use the make:migration
artisan command.
php artisan make:migration create_users_table
This generates a migration file in the database/migrations
directory. Inside the up
method, define the table schema.
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
public function up()
{
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->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
Run the migration:
php artisan migrate
2. Changing Column Name
To rename a column, you must use the renameColumn
method provided by Laravel.
- Generate a new migration:
php artisan make:migration rename_user_name_column
- Edit the migration file:
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'full_name');
});
- Run the migration:
php artisan migrate
3. Changing Column Type
Laravel uses the doctrine/dbal
package to modify column types.
- Install the package:
composer require doctrine/dbal
- Create a migration:
php artisan make:migration change_user_name_column_type
- Modify the column type:
Schema::table('users', function (Blueprint $table) {
$table->text('name')->change();
});
- Run the migration:
php artisan migrate
4. Removing a Column
To drop a column, use the dropColumn
method.
- Generate a migration:
php artisan make:migration drop_user_column
- Remove the column:
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('email_verified_at');
});
- Run the migration:
php artisan migrate
5. Adding Indexing
Indexes improve query performance. Laravel supports different types of indexes:
- Unique Index
$table->string('email')->unique();
- Composite Index
$table->index(['name', 'email']);
- Dropping an Index
$table->dropIndex(['name_email_index']);
6. Foreign Keys
Foreign keys enforce relationships between tables.
- Add a foreign key:
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
- Remove a foreign key:
Schema::table('posts', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropColumn('user_id');
});
Summary Table
Task | Command | Method |
---|---|---|
Create Migration | php artisan make:migration | Schema::create |
Rename Column | php artisan make:migration | renameColumn |
Change Column Type | composer require doctrine/dbal | change() |
Remove Column | php artisan make:migration | dropColumn |
Add Index | - | unique() , index() |
Foreign Keys | - | foreign() , dropForeign() |
These tips will help you manage and modify your Laravel database schema effectively.