Migrations in Laravel provide a way to modify and manage your database schema over time. To create a new migration, follow these steps:
1. Run the Artisan Command
Use the Artisan CLI to create a migration file:
bashCopy codephp artisan make:migration create_users_table
Here, create_users_table
is the name of the migration, and this command generates a migration file in the database/migrations
folder.
2. Define the Schema
Inside the generated migration file, you can define the table structure using the up
method. Laravel provides schema building methods:
phpCopy codepublic function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
}
The down
method is used to reverse the migration, like dropping the table:
phpCopy codepublic function down()
{
Schema::dropIfExists('users');
}
3. Run the Migration
Once the migration file is ready, run the migration using:
bashCopy codephp artisan migrate
This will execute the migration and modify the database accordingly.
4. Roll Back Migrations
If you need to undo the migration, you can use:
bashCopy codephp artisan migrate:rollback
This will reverse the most recent migration.
Conclusion
Creating and managing migrations in Laravel is a simple and structured way to evolve your database schema over time. With the make:migration
command, you can easily define new tables, columns, and relationships, keeping your schema versioned and consistent.
ऑनलाइन वीडियो डाउनलोडर apk
Your writing has a way of resonating with me on a deep level. I appreciate the honesty and authenticity you bring to every post. Thank you for sharing your journey with us.