laravelpackageautoloadlaravel-seedinglaravel-package

Run Laravel Seeder From Package


I'm working on a package for an internal Laravel application and I'm having trouble running a seeder that exists in the package directory.

In my package's composer.json file located in packages/vendor/packagename, I've added the following:

"autoload": {
    "psr-4": {
        "Vendor\\PackageName\\": "src/",
        "Vendor\\PackageName\\Database\\Factories\\": "database/factories/",
        "Vendor\\PackageName\\Database\\Seeders\\": "database/seeders/"
    }
},

I have the following file located in "packages/vendor/packagename/database/seeders/DepartmentSeeder.php"

<?php

namespace Vendor\PackageName\Database\Seeders;

use Illuminate\Database\Seeder;
use Vendor\PackageName\Models\Department;

class DepartmentSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Department::factory()->count(10)->create();
    }
}

I then attempt to run the following command:

$ php artisan db:seed --class="Vendor\\PackageName\\Database\\Seeders\\DepartmentSeeder"

Target class [Vendor\PackageName\Database\Seeders\DepartmentSeeder] does not exist.

If I move the seeders directory into my src directory and run the following, it works, but I'd rather keep my seeders in my database directory.

$ php artisan db:seed --class="Vendor\\PackageName\\seeders\\DepartmentSeeder"

Does anyone have any idea why the class isn't being found? All my Google search results are purple and I even went to page two =/

Solution

In my particular case, composer dump-autoload wasn't doing the trick. What I ended up doing was running composer update vendor/packagename and whatever the issue was, it was resolved.

Hopefully, this helps anyone else who may have similar issues.


Solution

  • In my particular case, composer dump-autoload wasn't doing the trick. What I ended up doing was running composer update vendor/packagename and whatever the issue was, it was resolved.

    Hopefully, this helps anyone else who may have similar issues.