Search code examples
laraveltestingfactorylumen

Call to undefined function factory()


Environment: Laravel Framework Lumen (8.2.2) (Laravel Components ^8.0)

When I run

$blogs = factory('App\Blog', 2)->create();

in BlogsControllerTest.php, it shows

Call to undefined function factory()


Solution

  • As Laravel's upgrade guide says the Model factory was changed. The new way is like this App\Models\Blog::factory()->count(3)->create();

    To use old version referrer to documentation.

    However, to ease the upgrade process, a new laravel/legacy-factories package has been created to continue using your existing factories with Laravel 8.x

    To install it use composer composer require laravel/legacy-factories