laravel nwidart 模块内的工厂

laravel factories inside nwidart modules

我正在尝试在模块中使用 laravel 工厂。

我这样调用控制器内部的工厂

$ar_reference = factory(ArReference::class)->create();

这是我的工厂。

use App\Models\ArReference;
use Faker\Generator as Faker;

$factory->define(ArReference::class, function (Faker $faker) {

    return [
        'reference_no' => 'My Ar'
    ];
});

我在我的模块 ServiceProvider ArServiceProvider 中这样调用工厂,在 registerFactories() 函数中这样调用。

public function registerFactories() {

        $this->app->singleton(Factory::class, function () {
            return Factory::construct(__DIR__ . '/Database/factories');
        });
}

但不幸的是我遇到了这个错误

Argument 1 passed to Illuminate\Database\Eloquent\Factory::construct() must be an instance of Faker\Generator, string given, called in /var/www/Modules/Ar/Providers/ArServiceProvider.php on line 94

您需要在第一个参数中传递Faker生成器,参见:https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Factory.html#method_construct

return Factory::construct(\Faker\Factory::create(), __DIR__ . '/Database/factories');