Migrator subclass does not work for name migration

I have a migration with names, and I can not get past the errors of the Not Found class due to the namespace. In an earlier question , Antonio Carlos Ribeiro stated:

Laravel migrator does not play well with name migrations. It is best in this case to subclass and replace the Migrator class, as Christopher Pitt explains in his blog post: https://medium.com/laravel-4/6e75f99cdb0 . p>

I tried to do this (it is followed by composer dump-autoload , of course), but I keep getting errors of the Not Found class. I have project files created as

 inetpub |--appTruancy |--database |--2015_04_24_153942_truancy_create_districts.php |--MigrationsServiceProvider.php |--Migrator.php 

The migration file itself is as follows:

 <?php namespace Truancy; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class TruancyCreateDistricts extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('districts', function($table) { $table->string('id')->unique()->primary()->nullable(false); $table->string('district'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('districts'); } } 

Migrator.php is as follows:

 namespace Truancy; use Illuminate\Database\Migrations\Migrator as Base; class Migrator extends Base{ /** * Resolve a migration instance from a file. * * @param string $file * @return object */ public function resolve($file) { $file = implode("_", array_slice(explode("_", $file), 4)); $class = "Truancy\\" . studly_case($file); return new $class; } } 

MigrationServiceProvider.php is as follows:

 <?php namespace Truancy; use Illuminate\Support\ServiceProvider; class TruancyServiceProvider extends ServiceProvider{ public function register() { $this->app->bindShared( "migrator", function () { return new Migrator( $this->app->make("migration.repository"), $this->app->make("db"), $this->app->make("files") ); } ); } } 

The lines created in the autoload_classmap.php file are as expected:

 'Truancy\\Migrator' => $baseDir . '/appTruancy/database/migrations/Migrator.php', 'Truancy\\TruancyCreateDistricts' => $baseDir . '/appTruancy/database/migrations/2015_04_24_153942_truancy_create_districts.php', 'Truancy\\TruancyServiceProvider' => $baseDir . '/appTruancy/database/migrations/MigrationsServiceProvider.php' 

I call php artisan migrate --path="appTruancy/database/migrations" and I get an error:

 PHP Fatal error: Class 'TruancyCreateDistricts' not found in C:\inetpub\laravel\vendor\laravel\framework\src\Illuminate\Database \Migrations\Migrator.php on line 297 

I know that I have to do something dumb (my instinct is $class = "Truancy\\" . studly_case($file); in Migrator.php is incorrect), but I can not unscrew this light bulb. The migrate command obviously finds my migration file successfully, and the correct class name is in classmap, so it should be somewhere in the process of resolving the class name itself from the file that the subclass and substitution should reference. Any suggestions as to where I went wrong?

+1
namespaces laravel-4 laravel-migrations illuminate-container
source share
1 answer

Ok, I got this job. It turns out that the Medium.com article assumes that you just know where to place the files that he is talking about, which I did not know. I made a few changes, and now everything works correctly:

  • I created a new subfolder appTruancy\providers and add it to composer.json
  • I moved both Migrator.php and MigrationServiceProvider.php to a new folder
  • I changed the namespace in both of these files to Truancy\Providers to match the directory structure
  • I added 'Truancy\Providers\MigrationsServiceProvider' to the providers array in appTruancy\config\app.php
  • I added \ in front of Schema in the migration file to reference the base namespace.
  • I ran dump-autoload to update classmap

This is one of those cases when I am not 100% sure that all changes are necessary, but the layout makes sense, so I am pleased with this. So, in short, if you are trying to list your migrations, you need to subclass the Migrator class as described in the Medium.com article above, but then you need to register the service provider in config\app , making sure that the class names in both files are consistent.

0
source share

All Articles