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?