How do Laravel migrations work?

后端 未结 3 1009
时光取名叫无心
时光取名叫无心 2020-12-16 17:50

I\'m totally new to this type of framework. I\'ve come from barebones PHP development and I can\'t seem to find an easy to understand guide what migrations actually do.

3条回答
  •  别那么骄傲
    2020-12-16 18:53

    A simple explanation of migrations:

    They're a versioning system for your database scheme.

    Imagine you're setting up a new application. The first thing you do is create a table (call it mytable) with a couple of columns. This will be your first migration. You run the migration (php artisan migrate) when you start working on your application and voila! You have a new table in your database.

    Some time later, you decide that you need a new column in your table. You create a migration (php artisan make:migration in Laravel 5) and a new migration file is created for you. You update the code in that migration, run php artisan migrate again, and you have a new column in your table.

    Ok, that's the basic idea behind migrations. But there's more...

    Suppose, later on, you realize that you messed up when you created that last migration. You've already run it, so your database has changed. How to you fix it? Well, if you wrote your migration correctly and implemented the down method, you can just do php artisan migrate:rollback and it will rollback the migration.

    How does Laravel do this? By keeping track of your migrations in a special database table. The php artisan migrate:install command will set things up for you so Laravel can manage these migrations.

    Over time, as your application grows, you will add more and more migrations. Laravel gives you a way to step forward and back through your migrations as needed to ensure your database is at whatever state you need it to be as you're working.

    Check out the list of artisan commands with php artisan. You can also request help on a particular command with php artisan help .

提交回复
热议问题