10

I namespaced my whole package successfully but I can not get namespaced migrations to work. In the autoload_classmap.php the migration classes are nicely namespaced, but the Migrator is not looking for the migration classes within the namespace. How to get the migrator to search for the migrations within the namespace?

The migration file

<?php namespace Atomend\Aeuser;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Schema, User;

class UsersTable extends Migration {

   public function up() {
      Schema::create("users", function(Blueprint $table) {

         $table
            ->increments("id");

autoload_classmap.php

'Atomend\\Aeuser\\UsersTable' => $baseDir . '/src/migrations/2014_04_21_184359_users_table.php',

Terminal error

PHP Fatal error:  Class 'UsersTable' not found in

This is logical since the UsersTable is in the Atomend\Aeuser namespace.

Issuing the migration

php artisan migrate --bench="atomend/aeuser"`

So to be clear, when losing the namespace everything works fine and dandy.

Laurence
  • 58,936
  • 21
  • 171
  • 212
sidneydobber
  • 2,790
  • 1
  • 23
  • 32
  • How did you create your migration file? – majidarif Apr 25 '14 at 18:42
  • Can you include your command for executing the migration? is your work still on the workbench? – majidarif Apr 25 '14 at 19:04
  • Did you try using the `--path` ? `php artisan migrate --path=app/foo/migrations` – majidarif Apr 25 '14 at 19:13
  • did you try to run `composer dump-autoload` on the workbench root and the laravel root folders? – majidarif Apr 25 '14 at 19:14
  • That doesn't work, also there is nothing wrong with the autoload_classmap.php. As you can see in the sample code it's in the proper namespace. – sidneydobber Apr 25 '14 at 19:16
  • If you are working with HFS+ or ext3 filesystem, you can put the namespace in the name of the file, look for: https://github.com/girotecnics/geonames/tree/master/src/database/migrations – corretge Jul 25 '17 at 13:22

2 Answers2

12

Laravel migrator doesn't play nice with namespaced migrations. Your best bet in this case is to subclass and substitute the Migrator class, like Christopher Pitt explains in his blog post: https://medium.com/laravel-4/6e75f99cdb0.

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • Hey Antionio, nice to get some feedback on this topic! I will have a look at your suggestion, maybe a nice feature for a pull request. – sidneydobber Apr 30 '14 at 13:48
  • Obviously a late addition, but I discovered that Christopher Pitt's article was short a couple of details that threw me off, and I've posted what was missing in [another question](http://stackoverflow.com/questions/29855793/subclassing-migrator-not-working-for-namespaced-migration) – dspitzle Apr 27 '15 at 19:40
0

add "Database\Migrations\": "database/migrations/" to composer.json -> autoload:psr-4