What is Laravel Database Migration?
Laravel Migration provides a mechanism to propagate database changes from one machine to another (like development to production). It is a kind of version control of the database.
Create an application after installing laravel by using composer (like in the example below application is created using the command- composer create-project –prefer-dist laravel/laravel myproject)
Now create a database-(for this example- create database mydb;)
Connect your database with your application from /config/database.php file.
Steps for migration:(with screenshots)
1. Create migration table: Create a migration table in laravel database by using this command-
$ php artisan migrate:install
If you get this error:
Then change your .env file-
Then again try to run this command
2. Create migration files: This will create migration files in /database/migrations folder where you can give schema or data for tables.
$ php artisan make:migration create_products_table
You will see a file with name xxxx_xx_xx_xxxxxx_create_products_table.php like-
Migration structure contain 2 default functions-
up-> executed when migration run
down-> executed when migration rolls back.
3. Create db table using migration: Give schema in up function in migration file:
Now, run following command to create table in database:
$ php artisan migrate
If you get this error while running this command on terminal, follow following steps:
Add marked lines in this screenshot in /app/Providers/AppServiceProvider.php.
Add this marked line in /database/migrations/xxxx_xx_xx_xxxxxx_create_users_table.php file.
Now again run php artisan migrate command on terminal.
3 tables will be created in your database. Open phpmyadmin and check your table, it will contain users, password_resets and products tables.
Screenshot of products table:
4. Rollback using migration: To rollback any changes done using migration, run this command in terminal:
$ php artisan migrate:rollback
This will run all down functions from migration files. Now, open phpmyadmin, you will see that there is no table in your database.
5. Add/Remove columns using migrations: Run the following command to create a new migration file in /database/migrations folder.
$ php artisan make:migration add_brand_to_products --table=products
You will a new file xxxx_xx_xx_xxxxxx_add_brand_to_products.php that will look like:
Add following code to add ‘brand’ column in products table:
Now run this command to make changes in database table:
$ php artisan migrate
Open your phpmyadmin, you will see a new column ‘brand’ in products table.
6. Change column type: Run this command to change type of column:
$ php artisan make:migration modify_item_name_in_products --table=products
This command will create a migration file where you can modify columns of products table.
In migration file, add following code marked in below screenshot.
In up function,
maintains the varchar data type and sets the character limit to 50.
In down function,
will rollback the migration to the previous state.
After writing code to modify the table columns, run php artisan migrate command in terminal to make changes in your database.
In case you get following error:
You need to install Doctrine Database Abstract Layer DDBAL in Laravel before we can change columns. DDBAL is used for Laravel migration alter table tasks.
So run this command from terminal to install it:
$ composer require doctrine/dbal
You will see following process during installation. Once done with this. Again run php artisan migrate command.
Now the changes will be reflected in products table in phpmyadmin database.