Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually name Laravel migration so "php artisan migrate" can run it?

Tags:

php

laravel

I have a side project written in PHP that can automatically generate a migration file based on the information from my audit table. Since the file is created by my application without using the artisan command, I have no idea how to make Laravel to see my migration files.

A sample of a migration file my application generated:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class sampleMigration extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('audit_database_index_column_relations', function(Blueprint $table){
        $table->integer('relation_id');
        $table->integer('database_index_id');
        $table->integer('database_column_id');
        $table->integer('sequence_position');
        $table->integer('is_deleted');
});
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('audit_database_index_column_relations');
    }
}

Regarding what I have attempted, I firsly tried to specify the path linking to my migration file. However, it showed an error that is

Symfony\Component\Debug\Exception\FatalThrowableError  : Class '' not found

  at C:\Users\L0tusw0w\Desktop\Laravel\testProject\vendor\laravel\framework\src\Illumi
nate\Database\Migrations\Migrator.php:448
    444|     public function resolve($file)
    445|     {
    446|         $class = Str::studly(implode('_', array_slice(explode('_', $file), 4)
));
    447|
  > 448|         return new $class;
    449|     }
    450|
    451|     /**
    452|      * Get all of the migration files in a given path.

  Exception trace:

  1   Illuminate\Database\Migrations\Migrator::resolve("sampleMigration")
      C:\Users\L0tusw0w\Desktop\Laravel\testProject\vendor\laravel\framework\src\Illum
inate\Database\Migrations\Migrator.php:186

  2   Illuminate\Database\Migrations\Migrator::runUp("C:\Users\L0tusw0w\Desktop\Larave
l\testProject/database/migrations/sampleMigration.php")
      C:\Users\L0tusw0w\Desktop\Laravel\testProject\vendor\laravel\framework\src\Illum
inate\Database\Migrations\Migrator.php:162

In my second attempt, I replaced the up() function of the default migration "create_user_table" with my up() function as the example, and as I expected, it executed without any problem! In general, I suppose that the error must be from my way of naming the file.

Therefore, I would like to know how I can name my migration file in a way so that Laravel can detect and run them

Thank you in advance!

like image 580
LoTus96 Avatar asked Dec 07 '25 05:12

LoTus96


1 Answers

The error message you're getting when trying to run your migration actually provides the answer you need in this case. The function is using the name of the file to determine what the name of the class for the migration should be. We read chained functions like this from the inside most paranthesis out so starting at the beginning

explode('_', $file)

The method takes the file name and explodes it into an array, splitting on under scores. So

2014_10_12_100000_create_password_resets_table

becomes

[ "2014", "10", "12", "100000", "create", "password", "resets", "table", ]

Next, the method slices off the first 4 elements of the array. These elements contain a timestamp of when the migration was created, to determine the order to run the migrations, but have nothing to do with the class name

array_slice(explode('_', $file), 4)

So now we have

[ "create", "password", "resets", "table", ]

next the array gets collapsed back into a string, with underscores separating each word

implode('_', array_slice(explode('_', $file), 4))

giving us

create_password_resets_table

And lastly the Laravel Str::studly helper function converts this new string to studly case to determine the class name

Str::studly(implode('_', array_slice(explode('_', $file), 4)));

CreatePasswordResetsTable

So to create a migration file name, start with a timestamp of when you are creating the file, then add the name of the class in all lowercase, separated by underscores.

Class SampleMigration extends Migration

Would be

2019_12_04_37860000_sample_migration

Special note: Don't skip the timestamp or try to throw whatever you want in those first 4 elements. The order that the files appear in the directory is the order the migrations will be run in, the timestamps ensure that migrations are always run in the right order. Skipping this step could cause migrations that change tables to run before the table is created, or changes running out of order and giving you an unexpected result for the current table.

like image 83
Alec Joy Avatar answered Dec 08 '25 18:12

Alec Joy