1

I want to use some fake data with Factory so at my ArticleFactory I added this:

public function definition()
    {
        return [
            'title' => $faker->text(50),
            'slug' => $faker->slug(),
            'body' => $faker->paragraph(rand(5,20)),
        ];
    }

And then at web.php I added this:

use App\Models\Article;
Route::get('/', function () {
    factory(Article::class,10)->create();
});

But now I get this error:

Error Call to undefined function factory()

So what's going wrong here? How can I fix it?

  • 1
    In Laravel 8, the factory helper is no longer available. Your Thread model class should use HasFactory trait, then you can use your factory. However, if you prefer to use the Laravel 7.x style factories, you can use the package `laravel/legacy-factories` You may install it with composer: `composer require laravel/legacy-factories` – STA Feb 06 '21 at 08:32

1 Answers1

4

In Laravel 8, the factory helper is no longer available. Your Thread model class should use HasFactory trait, then you can use your factory.

Change to :

\App\Models\Article::factory()->count(10)->create();

However, if you prefer to use the Laravel 7.x style factories, you can use the package laravel/legacy-factories You may install it with composer:

composer require laravel/legacy-factories
STA
  • 30,729
  • 8
  • 45
  • 59