我很欣赏这个问题确实出现在这里很多次,但是在查看答案并试图解决问题后,遗憾的是它仍然存在。
我正在使用Laravel 5.6的Caffeinated Modules for Laravel包。我创建了一个包含以下内容的用户模块。
UserTableSeeder App / Modules / User / Database / Seeds / UserTableSeeder.php
<?php
namespace App\Modules\User\Database\Seeds;
use Illuminate\Database\Seeder;
use App\Modules\User\Models\User;
class UserTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(User::class, 3)->create();
}
}
UserFactory App / Modules / User / Database / Factories / UserFactory.php
<?php
use Faker\Generator as Faker;
use App\Modules\User\Models\User;
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
$factory->define(User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => str_random(10),
];
});
UserDatabaseSeeder App / Modules / User / Database / Seeds / UserDatabaseSeeder.php
<?php
namespace App\Modules\User\Database\Seeds;
use Illuminate\Database\Seeder;
class UserDatabaseSeeder extends Seeder
{
public function run()
{
$this->call(UserTableSeeder::class);
}
}
用户 App / Modules / User / Models / User.php
<?php
namespace App\Modules\User\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
当我跑步时
UserDatabaseSeeder 调用 UserTableSeeder ,但会产生以下错误消息:php artisan module:seed
播种:App \ Modules \ User \ Database \ Seeds \ UserTableSeeder
InvalidArgumentException:无法找到名称为factory的工厂 [默认] [App \ Modules \ User \ Models \ User]。
非常感谢任何帮助。
答案 0 :(得分:0)
基本上,加盖模块不支持从您的模块开箱加载工厂。
我发现此问题包含修复:https://github.com/caffeinated/modules/issues/337
将此添加到您的ModuleServiceProvider
:
/**
* Register the module services.
*
* @return void
*/
public function register()
{
$this->app->register(RouteServiceProvider::class);
$this->mergeConfigFrom(
__DIR__.'/../config.php',
'user'
);
$this->registerEloquentFactoriesFrom(__DIR__.'/../Database/Factories');
}
/**
* Register factories.
*
* @param string $path
* @return void
*/
protected function registerEloquentFactoriesFrom($path)
{
$this->app->make(Factory::class)->load($path);
}