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

jeroenvanrensen's avatar

Laravel Database Seeding Class not Found

Hi everyone,

I've got a database seeder class, which I want to use in my PHPunit test. However, I got this message: Target class [database\seeds\RetailerWithProduct] does not exist.

This is my Database Seeder

<?php

use Illuminate\Database\Seeder;

class RetailerWithProduct extends Seeder
{
    public function run()
    {
        $product = Product::create([
            'name' => 'Nintendo Switch'
        ]);

        $retailer = Retailer::create([
            'name' => 'Best Buy'
        ]);

        $stock = new Stock([
            'price' => 10000,
            'url' => 'https://www.example.org/',
            'sku' => 12345,
            'in_stock' => false
        ]);

        $retailer->addStock($product, $stock);
    }
}

And here's my PHPunit test:

/** @test */
public function it_tracks_product_stock()
{
    $this->withoutExceptionHandling();

    $this->seed(RetailerWithProduct::class);

    dd(Product::all());

    $this->assertFalse($stock->fresh()->in_stock);

    Http::fake(function() {
        return [
            'available' => true,
            'price' => 29900
        ];
    });

    $this->artisan('stock:track');

    $this->assertTrue($stock->fresh()->in_stock);
}

Two things are getting my attention:

  • I do not import the Database Seeder in my test
  • The Database Seeder doens't have a namespace

But I'm watching a Laracasts series and Jeffrey also doesn't one of these.

I think I have everything the same, but why do I get this error?

Thank you! Jeroen

If you need more information, please ask me.

PS: I also ran composer dump-autoload

0 likes
7 replies
GamerFac3's avatar

Try adding a backslash like so$this->seed(\RetailerWithProduct::class);

jeroenvanrensen's avatar

I tried it and it changed the error:

ErrorException: include(C:\wamp64\www\laravel\stock-tracker\vendor\composer/../../database/seeds/RetailerWithProduct.php): failed to open stream: No such file or directory
BezhanSalleh's avatar

I run into this but didn’t really search for the exact solution but you can try the following: Also try renaming the seeder as ‘RetailerWithProductSeeder’

php artisan db:seed --class=RetialerWithProduct

Cheers!

jeroenvanrensen's avatar

Hi @gamerfac3, @bezhansalleh,

I changed the name and ran this command:

php artisan db:seed --class=RetialerWithProductSeeder

Now I got this error:

   Illuminate\Contracts\Container\BindingResolutionException

  Target class [RetialerWithProductSeeder] does not exist.

  at C:\wamp64\www\laravel\stock-tracker\vendor\laravel\framework\src\Illuminate\Container\Container.php:807
    803|
    804|         try {
    805|             $reflector = new ReflectionClass($concrete);
    806|         } catch (ReflectionException $e) {
  > 807|             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
    808|         }
    809|
    810|         // If the type is not instantiable, the developer is attempting to resolve
    811|         // an abstract type such as an Interface or Abstract Class and there is

  1   C:\wamp64\www\laravel\stock-tracker\vendor\laravel\framework\src\Illuminate\Container\Container.php:805
      ReflectionException::("Class RetialerWithProductSeeder does not exist")

  2   C:\wamp64\www\laravel\stock-tracker\vendor\laravel\framework\src\Illuminate\Container\Container.php:805
      ReflectionClass::__construct("RetialerWithProductSeeder")

I really don't know what to do here.

BezhanSalleh's avatar
Level 25

you have a spelling mistake in your class and its file name

//RetailerWithProductSeeder.php

<?php

use App\Product;
use App\Retailer;
use App\Stock;
use Illuminate\Database\Seeder;

class RetailerWithProductSeeder extends Seeder
{
    public function run()
    {
        $product = Product::create([
            'name' => 'Nintendo Switch'
        ]);

        $retailer = Retailer::create([
            'name' => 'Best Buy'
        ]);

        $stock = new Stock([
            'price' => 10000,
            'url' => 'https://www.example.org/',
            'sku' => 12345,
            'in_stock' => false
        ]);

        $retailer->addStock($product, $stock);
    }
}

then

php artisan db:seed --class=RetailerWithProductSeeder
1 like
jeroenvanrensen's avatar

Hi @bezhansalleh,

I tried your code and it's finally working, so thanks!

I don't see a spelling mistake, I only see the imports I seems to be forgotten.

Do you know why I didn't get a more specific error?

Thank you! Jeroen

BezhanSalleh's avatar

@jeroenvanrensen the one thing i can think of is since you are not using the prefered way of creating seeders. I know sometimes we need a dirty quick way to get away with stuff and get our desired functionality and creating seeders for each model gets kinda hectic but in the long run it helps you and gives more control

for your scenario i would have started with following:

//factories
ProductFactory
StockFactory
RetailerFactory

//seeders
ProductSeeder
StockSeeder
RetailerSeeder

//DatabaseSeeder
    public function run()
    {
        $this->call([ProductSeeder::class]);
        $this->call([StockSeeder::class]);
        $this->call([RetailerSeeder::class]);
    }

//Observers

class RetailerObserver
{
    public function created(Retailer $retailer)
    {
	$retailer->addStock(App\Product::first(), App\Stock::first());
    }
}

Please or to participate in this conversation.