Try adding a backslash like so$this->seed(\RetailerWithProduct::class);
Jul 18, 2020
7
Level 15
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
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
Please or to participate in this conversation.