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 { public function up() { Schema::create('districts', function($table) { $table->string('id')->unique()->primary()->nullable(false); $table->string('district'); }); } public function down() { Schema::drop('districts'); } }
Migrator.php is as follows:
namespace Truancy; use Illuminate\Database\Migrations\Migrator as Base; class Migrator extends Base{ 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?