Migrations in Laravel are a type of version control for databases. They allow developers to manage and modify the structure of their databases using PHP code, rather than manually writing SQL statements. Migrations provide a way to define database schema changes in a series of incremental steps, making it easier to modify and share database structure changes across different environments and team members.
Migrations in Laravel are essentially PHP classes that extend the base Migration
class provided by the framework. These classes contain two main methods: up
and down
. The up
method defines the actions to be taken when migrating 'up', such as creating a new table or adding a column, while the down
method defines how to reverse those changes, such as dropping a table or removing a column.
Migrations help manage database schema changes in Laravel in several ways:
The process of creating and running a migration in Laravel involves several steps:
Create a Migration: To create a new migration, you can use the make:migration
Artisan command. For example, to create a migration that adds a new table called users
, you would run:
<code>php artisan make:migration create_users_table</code>
This command will create a new PHP file in the database/migrations
directory.
Edit the Migration File: Open the newly created file and edit the up
and down
methods to define the schema changes. For instance, to create a users
table, your up
method might look like this:
public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamps(); }); }
The down
method should reverse these changes:
public function down() { Schema::dropIfExists('users'); }
Run the Migration: To apply the changes to the database, use the migrate
Artisan command:
<code>php artisan migrate</code>
This command will execute the up
method of all pending migrations, updating the database schema accordingly.
Yes, migrations in Laravel can be rolled back. This feature is particularly useful during development when you might need to undo recent schema changes. To roll back the last migration, you can use the rollback
Artisan command:
<code>php artisan migrate:rollback</code>
This command will execute the down
method of the most recent migration, reversing the changes it made to the database.
If you need to roll back multiple migrations, you can specify the number of migrations to roll back using the --step
option. For example, to roll back the last three migrations, you would run:
<code>php artisan migrate:rollback --step=3</code>
In addition to rolling back migrations, Laravel provides the migrate:reset
command, which rolls back all migrations:
<code>php artisan migrate:reset</code>
This will undo all migrations and leave your database in its initial state. If you want to roll back all migrations and then re-run all migrations, you can use the migrate:refresh
command:
<code>php artisan migrate:refresh</code>
This is equivalent to running migrate:reset
followed by migrate
.
The above is the detailed content of What are migrations in Laravel?. For more information, please follow other related articles on the PHP Chinese website!