Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

automica's avatar

Unable to locate factory with name but can access model

Hi, I'm working on an application using laravel 5.8 running on local docker php7.2.

Recently, I've been getting an issue relating to factories where factories using models where $connection is specified throws the Unable to locate factory with name [default] error.

This is question is well asked on laracasts, and usually is answered by ensuring namespace is correct.

In my case,

  • I am able to connect to the model, which would imply $connection and .env variables are correct.
  • I am able to call factories within the application that use the default connection.

in my route:

Route::get('check', function () {

    dump(config('database.connections.mysql'));
    dump(config('database.connections.external'));

    $countryCodes = \App\Models\External\CountryCodes::all()->first();
    $countryCodesFactory = factory(\App\Models\External\CountryCodes::class)->create();

    return [
        $countryCodes, $countryCodesFactory
    ];
});

my model

<?php

declare(strict_types=1);

namespace App\Models\External;

use Illuminate\Database\Eloquent\Model;

/**
 * Class CountryCodes
 *
 * @property int $id
 * @property string $iso2
 * @property string $name
 * @property bool $has_postcode
 * @property bool $eu_member
 * @property bool $eea_member
 */
class CountryCodes extends Model
{
    /**
     * @var bool
     */
    public $timestamps = false;

    /**
     * @var string
     */
    protected $connection = 'external';

    /**
     * @var string
     */
    protected $table = 'country_codes';

    /**
     * @var string[]
     */
    protected $casts = [
        'has_postcode' => 'boolean',
        'eu_member' => 'boolean',
        'eea_member' => 'boolean',
    ];
}

and factory

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Models\External\CountryCodes;
use Faker\Generator as Faker;

$factory->define(CountryCodes::class, function (Faker $faker) {
    return [
        'iso2' => 'GB',
        'name' => 'United Kingdom',
    ];
});

To add to my frustration, when i push the code to the pipeline (also running php7.2), all tests pass without any issues with the factories.

This would suggest this issue is environmental. My initial thoughts were that it was connection related but given i am able to connect to the external database (running on the same host) then i'm at a loss.

  • Caches have been cleared (running php artisan optimize:clear).
  • I've deleted packages.php and services.php within bootstrap/cache.
  • vendor packages have been deleted and reinstalled using composer.

Is there some other cache that I need to delete to tell my application about location of factories?

Any ideas?

0 likes
2 replies
automica's avatar

Looking within Illuminate\Database\Eloquent\FactoryBuilder

if i dump dd($this->definitions); within getRawAttributes method, I only see a few factories. There is one using external connection. if I try to call a factory that doesnt show in definitions then i get the error mentioned above.

automica's avatar

Seems theres something breaking the factories.

If I delete only the factories showing in dd($this->definitions) then all the factories in the factory/ directory before those now appear in my dump.

This will allow me to build out my new feature whilst I continue to investigate.

Please or to participate in this conversation.