How to use Artisan Migrate To Manage Laravel Databases

How to use artisan migrate to manage laravel databases

Laravel, the extremely popular PHP free and open-sourced web framework has many great features including the time-saving command line tool Artisan. Released in 2011, Laravel has grown in usage helping PHP developers from around the world build web applications faster and cleaner with the help of a large community and its codebase following the MVC (Model, View, Controller) architectural pattern. Based on the Symfony framework, which was released earlier back in 2005, which makes up a collection of reusable PHP components libraries has helped Laravel become what it is today. One of the core principles of Laravel is to give web developers the freedom to focus attention on building the core features of the application instead of re-coding the same sort of things, such as an ACL (access-control list) for user permissions. To aid this experience, Laravel is bundled with an application called Artisan. Shipped as a command line interface, Artisan comes with powerful commands that help with the day-to-day, such as interacting with the database that is connected to Laravel.

What is Artisan Migrate in Laravel?

Migrations in Laravel using the artisan command are what code version control is but for your database. It provides a way to manage changes over time, including changes such as creating and modifying database tables and columns. Particularly useful when working in a team, it allows everyone to share the same database schema, without the need to manually intervene to ensure you have table X or column Y. When running the migration Laravel will check each file in turn, checking to see if it has been run yet. The order of the files is important, especially when handling foreign keys, but luckily Laravel will handle that for you. Migration in Laravel can also include seeding the database with data, which is a great way to get up and running, especially when working on a development box. Let's explore each command line feature that makes up artisan migration.

Running Artisan Commands with PHP

As artisan is a command line tool, you'll need to run everything inside a terminal. The best way to check you're up and running is to use Artisan correctly, try running the following command from the location of your Lavaral project folder. Once your terminal window is inside the same root Lavarel folder, running the below command should display your current Laravel version. It should (without any errors or warnings) output Laravel Framework and then your version number.

php artisan -V

# Outputs
Laravel Framework x.xx.x

If however, you receive the following error; "Could not open input file: artisan" that likely means you're not in the root of the Lavaral folder. Once Lavaral has outputted your version number correctly, you can be safe in the knowledge that your artisan CLI tool is correctly running in PHP.

First up, to run any migration task on Laravel with Artisan, you will require the following command. The "[options]" part of the command is optional, and without any options, PHP will execute the Laravel migration.

php artisan migrate [options]

However, within the migration tool, there are various different options that can be added to the migrate command to output and perform different things. Let's explore each one and find out what they all do.

--database[=DATABASE]

Within the migration tool, it's possible to tell Artisan which database you'd like to connect to, in order to run the migration. This is particularly useful in a multi-tenant application setup.

--force

An often popular option to be run when this command is executed on a live production server. Here you'll want to use --force in order to force the operation. This is to prevent the CLI from asking for confirmation. This is normally part of an automated process especially when pushing new changes live, without the need for manual intervention.

--path[=PATH]

Here, --path allows you to specify the files and folder in which you would like Laravel to look. This option can be skipped unless you have a non-standard location for your database migrations. The format of this option will be something similar too; "--path=/httpdocs/myfolder".

--realpath

By specifying the realpath, you can provide Laravel with the absolute path of the migration files instead of the relative path. This can be useful if you're running the command not from the current working directory.

--schema-path[=SCHEMA-PATH]

Similar to "--path", the schema path lets you set the schema dump file generated by Artisan, prior to running the migration. This is a useful feature of Larvel for those projects where the migration folder is huge, by running "php artisan schema:dump", Laravel will squash your migrations into a single SQL file.

--pretend

Similar to dry-running, the pretend option will simulate the database migration. Useful for checking things will work before committing to changing the database for real.

--seed

A popular option, seed if used will indicate to Laravel that you wish to re-run database seeding. Seed is the term Laravel uses to populate your database with data. This normally consists of using a database seeder class with attributes provided to generate random data, such as names or email addresses.

--seeder[=SEEDER]

If you're looking to seed, but not using the default DatabaseSeeder class, you can specify your own here.

--step

If you are looking to rollback a migration you can by using the step flag. This would be similar to; "migrate:rollback --step=N" where N is the number of steps to rollback. Each rollback is each file you're reverting. You can provide the step flag to the "migrate:refresh" command also.

Laravel Migrate Commands

We've briefly mentioned rollback and refresh but migrate comes with a few more helpful commands to aid with database management. Let's explore each one and find out what they do.

migrate:fresh

Running the migrate fresh command to drop all tables and re-run all migrations again. This is to be used when you don't mind losing your current database's data and table schema.

migrate:install

Run this command to create the migration repository

migrate:refresh

Run this to reset and re-run all migrations from your Laravel application, and can be also used with the "--step" option.

migrate:reset

This command will rollback all database migrations

migrate:rollback

This command will roll back the last database migration and can be also used with the "--step" option.

migrate:status

A very useful command to find the current status of your database migration, showing the status of each.

Senior PHP developer. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience, and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all