What the F*ck Is With All the Artisan Commands: make:migration
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 --create
parameter:
$php artisan make:migration --create=new_table_name name_of_migration
Created Migration: 2019_09_13_012814_name_of_migration
And that produces the following PHP class:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class NameOfMigration extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('new_table_name', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('new_table_name');
}
}
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.
php artisan make:migration create_roles
Created Migration: 2019_09_12_013314_create_roles
This produces the following up function:
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
}
Altering an Existing Table
In order to alter an existing table you can run the command with --table
parameter:
/var/www$ php artisan make:migration --table=roles add_name_to_roles
Created Migration: 2019_09_12_013847_add_name_to_roles
Which generates the following PHP class.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddNameToRolesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('roles', function (Blueprint $table) {
//
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('roles', function (Blueprint $table) {
//
});
}
}
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 --table
argument.
const CHANGE_PATTERNS = [
'/_(to|from|in)_(\w+)_table$/',
'/_(to|from|in)_(\w+)$/',
];
So can run the following command and have it work without the --table
argument.
/var/www$ php artisan make:migration add_name_to_roles
Created Migration: 2019_09_12_013847_add_name_to_roles
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.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows