jhine
jhine

Reputation: 247

Laravel Caffeinated Modules Model Factory - Unable to locate factory with name

I appreciate this question does appear on here many times, however after looking through the answers and attempting to resolve the issue, it unfortunately still persists.

I am using the Caffeinated Modules for Laravel package with Laravel 5.6. I have created a User module which contains the following.

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

the UserDatabaseSeeder calls the UserTableSeeder but produces the following error message:

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

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

Any help is much appreciated.

Upvotes: 1

Views: 1065

Answers (1)

Dan Fletcher
Dan Fletcher

Reputation: 1228

Basically Caffinated Modules doesn't support loading factories from your module out of the box.

I found this issue that includes a fix: https://github.com/caffeinated/modules/issues/337

Add this to your 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);
}

Upvotes: 0

Related Questions