caffeinated/modules

Unable to locate factory with name [default] [App\Modules\User\Models\User].

jameshine opened this issue · 3 comments

Laravel 5.6
Caffeinated 4.3

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);
    }
}

User 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',
    ];
}

When I run

php artisan module:seed

I get the following:

Seeding: App\Modules\User\Database\Seeds\UserTableSeeder

InvalidArgumentException : Unable to locate factory with name [default] [App\Modules\User\Models\User].

#95 didn't work for me.

Laravel autoload database/factories via composer, caffeinated/modules not
it is necessary to load those files as with the config files in the ModuleServiceProvider

@arthvrian
What needs adding to the ModuleServiceProvider exactly? Having the same issue.
Thanks

@fordster78 I just send a PR to fix this, see #370