5

I am using Laravel 5.1 now.

The migrations class file generated by php artisan make:migration create_users_table --create=users command, would be like this:

<?php

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

class CreateUsersTable extends Migration
{
    // up and down ...
}

When I edit this migration class file and save it, PHP Code Sniffer in my sublime text will point out a non-standard message of 'Each Class must be in a namespace of at least one level'.

I got no answer in both [https://laravel.com/docs/5.1/migrations] and top batches search results. The related questions on stackoverflow doesn't clear me, either.

Any one knows the reason, or give me some hints? Thanks!

1 Answer 1

7

It's a design matter, basically. There are people out there using namespaced migrations. But the way migrations are loaded and stored in the migration database table, by the migrator, could be a problem to have them namespaced. If you inspect it a little you'll see that the name of the class is part of the name of the migration file and Laravel needs to use that name back to instantiate the migration class to execute it. Having it namespaced could, for example, lead to 2 different classes with the same base class name (class name minus the namespace), which would collide and be very difficult to find. A solution for that is to add the namespace to the migration file name, but they would have to name it something like

2015_12_16_164131_database_migrations|create_users_table.php

Which would translate to a class

Database\Migrations\CreateUsersTable

I'm using | to separate the namespace from the file (class) name, but the whole thing got kind of odd, dont' you think?

Also, as migrations need to be executed in a certain order, the date and timestamp in the beginning of the migration file name is mandatory, so, that's some of the whys.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not quite understand the 'solution' you mentioned above, is it implemented by laravel already, or just a example to explain 'if migrations class has a namespace'?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.