Let’s start with my favorite feature so far: database migrations. I’m going to start with this command because I think it’s one of the more critical components and because the next article will involve working with them. :-)
Why It Exists
Database migrations allow you to define changes to your database so you can easily apply the changes everywhere you maintain a copy of the database for the application. After working in a product with no database versioning I’m a huge fan of any system that does and it’s great that Laravel supports this out of the box.
When Should You Use It
Any time you’re creating a new table or editing the structure of an existing table.
Making a New Table
In order to make a new table you can run the command with
And that produces the following PHP class:
You can also be a little lazy and prefix your migration name with “create_” and it will infer the table name from the migration name.
This produces the following up function:
Altering an Existing Table
In order to alter an existing table you can run the command with
Which generates the following PHP class.
But we can also be lazy about this process. If we look into Illuminate\Database\Console\Migrations\TableGuesser we can find the following regular expressions which determine migration names that will automatically determine the
So can run the following command and have it work without the
I’m going to skip the PHP output for this because it’s identical to the output above. :-)
Hopefully this has been as helpful to you as it has to me and check back soon for more artisan commands.