调用未定义的函数工厂()

Call to undefined function factory()

环境:Laravel 框架 Lumen (8.2.2)(Laravel 组件 ^8.0)

当我运行

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

BlogsControllerTest.php中显示

调用未定义函数工厂()

回答我自己的问题以防对某人有帮助。

它也是这样工作的。

$blogs = BlogFactory::new()->count(2)->create();

参考:laravel.com/docs/8.x/database-testing#connecting-factories-and-models

正如 Laravel 的 upgrade guide 所说,模型工厂已更改。 新的方式是这样的App\Models\Blog::factory()->count(3)->create();

要使用旧版本引荐来源网址 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

要安装它,请使用 composer composer require laravel/legacy-factories