Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

getvma's avatar
Level 49

Migrations and Seeds from NameSpaced Folders?

Ho do you include migrations and seeds that are in their own paths, when using the regular artisan migrate and seed commands? I'd like to include the migrations and seeds in the following manner;

app{namespace}{module-name}{folders}

In the example above, here is where the migrations would be.. app\MyNameSpace\Page\Database\Migrations{migration files} app\MyNameSpace\Page\Database\Seeders{seeder files}

I realize that there is a whole subject on package development but I'd like to primarily just keep the bulk of the code base within its own folder.

0 likes
8 replies
bobbybouwmann's avatar

Try this

<?php 

class DatabaseSeeder extends Seeder 
{
    public function run() {
        $this->call('App\MyNameSpace\Page\Database\Migrations\SomeSeederClass');
    }
}
<?php namespace App\MyNameSpace\Page\Database\Migrations;

use Illuminate\Database\Seeder as Seeder;

Class SomeSeederClass extends Seeder 
{
    public function run () {
        // Your migration here
    }
}

Note: I did this out my head, so I hope it's correct!

1 like
getvma's avatar
Level 49

Yes that likely tackles the seeder, however, the migration is done differently, the MigrateCommand extends a BaseCommand which returns;

$this->laravel->databasePath().'/migrations';

So this is hard set to that folder in the database path. I have installed caffeinated/module package and it registers a service provider that binds a migration command via the console so Im starting to think that without hacking it there is no way to register additional locations for migration classes.

At first i had tried to simply add a classmap in "autoload" but "database" is there for other purposes other than migrations.

At least that's how I read it at the moment.

bobbybouwmann's avatar

Yeah that is a tough one! I have seriously no clue on how to get that working!

EdgarSedov's avatar

For those who wants to call custom namespaced seeders (but not run them on db:seed), u can call them directly using this approach:

php artisan db:seed --class=\\Database\\Seeders\\{namespace}\\{seederClassName}

for example,

php artisan db:seed --class=\\Database\\Seeders\\Users\\DevelopUserSeeder

^ works for laravel 8.x

9 likes
BlueC's avatar

@EdgarSedov Thanks for this. It's a shame/pain that the artisan make:seeder and artisan db:seed commands don't play nicely with namespaced seeders.

zoaddar's avatar

protected $connection = 'another_database';

public function up() {
    $this->down();
    Schema::create('users',  function (Blueprint $table) {
        $table->increments('id');
    });
}

public function down() {
    Schema::dropIfExists('users');
}

cmd: php artisan migrate:fresh --seed

This works for me. I am using larave 8.x

arex's avatar

artisan db:seed --class=\\App\\<NamespaceDir>\\<AnotherNamespaceDir>\\Database\\Seeders\\<SeederClass> This works for me in Laravel 11

Please or to participate in this conversation.