3

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.

Josh
  • 714
  • 2
  • 8
  • 20
  • 2
    Hi Josh ;). This is the [command source's code](https://github.com/laravel/framework/blob/61eac9cae4717699ecb3941b16c3d775820d4ca2/src/Illuminate/Database/Console/Seeds/SeedCommand.php#L96-L112). Maybe try adding `\\` at the beginning of the namespace so it is root namespace, see if that helps. – matiaslauriti Jan 05 '23 at 19:38
  • 1
    I don't know why one \ got stripped away, use double \ – matiaslauriti Jan 05 '23 at 20:16
  • 2
    I don't know exactly why, but I ran the compose update command `composer update vendor/packagename` and it's now working. Not sure why `composer dump-autoload` didn't work =/ – Josh Jan 05 '23 at 20:28
  • 2
    Hi @Josh if you add your own answer as an ... answer, then you can mark this question as answered. Also you'll get some points that way – UnderDog Jan 05 '23 at 21:33

1 Answers1

0

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.

Josh
  • 714
  • 2
  • 8
  • 20