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

Vidarious's avatar

Best way to load Seeds and Routes from a Package?

My ServiceProvider is getting ugly and not very Laravel like.

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //Load Project's routes.
        include __DIR__.'/../Routes/ProjectRoutesWeb.php';

        //Load Project's seeds.
        include __DIR__.'/../Database/Seeds/ProjectDatabaseSeeder.php';
        include __DIR__.'/../Database/Seeds/AppInit/StatusTableSeeder.php';
        include __DIR__.'/../Database/Seeds/AppInit/UserTableSeeder.php';
        include __DIR__.'/../Database/Seeds/AppInit/FlagTableSeeder.php';
        include __DIR__.'/../Database/Seeds/AppInit/RoleTableSeeder.php';
        include __DIR__.'/../Database/Seeds/AppInit/SettingTableSeeder.php';
        include __DIR__.'/../Database/Seeds/AppInit/PermissionTableSeeder.php';
        include __DIR__.'/../Database/Seeds/AppInit/ActivityTableSeeder.php';
        include __DIR__.'/../Database/Seeds/AppInit/UserRoleTableSeeder.php';
        include __DIR__.'/../Database/Seeds/AppInit/RolePermissionTableSeeder.php';
        include __DIR__.'/../Database/Seeds/AppInit/UserFlagTableSeeder.php';

        //Initialize Project's controller.
        $this->app->make('Project\Controllers\ProjectController');
    }

I'd like my package to require as little Laravel core modification as possible. Isn't there a better way to include Seeds , Routes, Repositories and such from packages?

0 likes
6 replies
Vidarious's avatar

@zachleigh, I'm already loading migrations according to the package instructions you linked. Unfortunately seeds do not get similar treatment. Perhaps that makes sense as seeds are mostly used in a development environment. However, in my case when the package gets installed by the user an initial seed must be performed with the migration so that the user management default super admin account is created as well as various app settings and default roles / permissions.

Also, the package documentation does not provide any clean methods to load routes.

1 like
willvincent's avatar

I agree with zachleigh.. create an artisan command for the user to run on install of your package.

Shirshak's avatar

He may want to run seed with one command php artisan db:seed . But the problem with laravel is how can it determine order of seed file. I guess this is only reason why laravel don't provide ability to register seed

NicolasDisi's avatar

You can use the same logic that is used in ServiceProvider. This way you can simply run: php artisan db:seed and it should run any seeders you have in your packages.

protected function loadSeeders($seed_list){
$this->callAfterResolving(DatabaseSeeder::class, function ($seeder) use ($seed_list) {
            foreach ((array) $seed_list as $path) {
                $seeder->call($seed_list);
                // here goes the code that will print out in console that the migration was succesful
            }
        });
    }

then you can create a list using a function that gets every class name you are using in your package. And put it inside, like this

$seed_list[] = 'Vendor/Name/Directory/ClassName'
 // inside your service provider 
boot()
{
$this->loadSeeders($seed_list)
}
2 likes

Please or to participate in this conversation.