Making a new Migration
Last updated on May 1st, 2021.
What is a migration?
Migration files are php classes describing database changes. These can be applied to the actual database using
artisan migration and related commands.
According to laravel's documetation, "Migrations are like version control for your database, allowing your team to define and share the application's database schema definition. If you have ever had to tell a teammate to manually add a column to their local database schema after pulling in your changes from source control, you've faced the problem that database migrations solve."
A quick word on Sail / docker
The commands on this page assume you are using Laravel Sail, and that you have setup an alias sail command. The commands displayed start with the
If you are not using docker at all, you should be able to replace
php and see these commands work.
If you are using sail but have not set up an alias command, you should be able to replace
Generating a migration file
You can generate a migration using:
sail artisan make:migration
It is recommended to refresh your memory on the options via:
sail artisan help make:migration
Generating a migration to create a table would look like this:
sail artisan make:migration --create=widgets create_widgets_table
If you want to modify an existing table rather than creating a new table, you could do something like this:
sail artisan make:migration --table=widgets alter_widgets_table_add_grommet_id_column
Editing the newly generated migration file
Either of the above commands commands will create a new file in the
database/migration folder, which you can then edit, and add code to add or modify columns as appropriate for your needs. Documentation for this can be found at Laravel: Available Column Types.
Establishing the specific column work you want to do is a complex topic. There are a number of related topics which will eventually be covered in this guide, but are not yet. They include database normalization and column naming best practices.