I have exactly the same issue as this Stackoverflow question, but none of the solutions provided works for me.
I too have been trying to follow exactly the same Laravel API tutorial, and got stumbled by the Laravel Seeder failure.
This is my model appModelsArticle.php
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Article extends Model
{
use HasFactory;
protected $table = 'articles';
protected $fillable = ['title', 'body'];
}
This is my seeder databaseseedersArticlesTableSeeder
<?php
namespace DatabaseSeeders;
use IlluminateDatabaseConsoleSeedsWithoutModelEvents;
use IlluminateDatabaseSeeder;
use appModelsArticle;
class ArticlesTableSeeder extends Seeder {
/**
* Run the database seeds.
*/
public function run(): void {
Article::truncate();
$faker = FakerFactory::create();
// And now, let's create a few articles in our database:
for ($i = 0; $i < 50; $i++) {
Article::create([
'title' => $faker->sentence,
'body' => $faker->paragraph,
]);
}
}
}
When I run "$ php artisan db:seed –class=ArticlesTableSeeder" it complains about ‘Class "appModelsArticle" not found’
I have cleared the cache and ran "$ composer dump-autoload" a few times but that didn’t help.
2
Answers
you can use below code for seeder
I would use Factotories in this case.
database/factories/ArticleFactory.php
database/seeders/ArticleSeeder.php
Then execute: