In Laravel, migrations are used to manage database schema changes. Each migration class contains two main functions: the up
method and the down
method. These functions define the actions to be performed when migrating the database (upward migration) and when rolling back the migration (downward migration).
up
method: Theup
method defines the actions to be executed when migrating the database schema. It typically includes creating tables, adding columns, or modifying existing schema structures. Theup
method should include the necessary SQL statements or Laravel's schema builder methods to make the required changes to the database.down
method: Thedown
method defines the actions to be executed when rolling back the migration, undoing the changes made in theup
method. It should revert the changes made in theup
method, typically by dropping tables, removing columns, or reverting schema modifications.
By having both the up
and down
methods, Laravel's migration system provides a way to migrate the database schema forward and backward, making it easy to apply and rollback changes as needed during development or deployment.
after migration that table, if you wanna add new cloumns u can use in down method
or creatae new migration
Sure! Here's an example of a down
method in a Laravel migration class along with the corresponding command:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
class AddColumnToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function ($table) {
$table->string('new_column');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function ($table) {
$table->dropColumn('new_column');
});
}
}
s
In this example, the up
method adds a new column called new_column
to the users
table. The down
method, on the other hand, removes the new_column
column using the dropColumn
method.
To run the migration and apply the changes, you can use the following command:
php artisan migrate
To rollback the migration and undo the changes made by the up
method, you can use the migrate:rollback
command:
php artisan migrate:rollback
This command will execute the down
method of the latest executed migration and revert the corresponding changes.
No comments:
Post a Comment