skip to Main Content

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


  1. you can use below code for seeder

        <?php
    
    namespace DatabaseSeeders;
    
    use FakerFactory;
    use IlluminateDatabaseConsoleSeedsWithoutModelEvents;
    use IlluminateDatabaseSeeder;
    use DB;
    
    class ArticlesTableSeeder extends Seeder
    {
        /**
         * Run the database seeds.
         */
        public function run(): void
        {
            $faker = Factory::create();
            for ($i = 0; $i < 50; $i++) {
                DB::table('articles')->insert([
                    [
                        'title' => $faker->sentence,
                        'body' => $faker->paragraph
                    ]
                ]);
            }
        }
    }
    
    Login or Signup to reply.
  2. I would use Factotories in this case.

    database/factories/ArticleFactory.php

    <?php
    
    namespace DatabaseFactories;
    
    use IlluminateDatabaseEloquentFactoriesFactory;
    
    class ArticleFactory extends Factory
    {
        public function definition()
        {
            return [
                'title' => fake()->words(2, true),
                'body' => fake()->words(5, true),
            ];
        }
    }
    

    database/seeders/ArticleSeeder.php

    <?php
    
    namespace DatabaseSeeders;
    
    use AppModelsArticle;
    use IlluminateDatabaseSeeder;
    
    class DatabaseSeeder extends Seeder
    {
        public function run()
        {
            //this will create 50 records in DB, no need to use for loop
            Article::factory(50)->create();
        }
    }
    

    Then execute:

    php artisan db:seed --class=ArticleSeeder
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search