Run Laravel Seeder From Package
Asked Answered
L

1

6

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.

Lawford answered 5/1, 2023 at 19:29 Comment(4)
Hi Josh ;). This is the command source's code. Maybe try adding `\` at the beginning of the namespace so it is root namespace, see if that helps.Howland
I don't know why one \ got stripped away, use double \Howland
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 =/Lawford
Hi @Lawford if you add your own answer as an ... answer, then you can mark this question as answered. Also you'll get some points that wayCalenture
L
2

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.

Lawford answered 6/1, 2023 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.