In Laravel 8, when I run a factory()->create() on a model that has a __construct, this code :
Route::get('/test', function (){
$doc = ClientDocument::factory()->create();
});
fails :
"SQLSTATE[HY000]: General error: 1364 Field 'filename' doesn't have a default value (SQL: insert into `client_documents` (`updated_at`, `created_at`) values (2021-11-16 12:45:20, 2021-11-16 12:45:20))"
If I remove the __construct, the factory runs fine and saves to the database... What am I missing here? Thanks!
MODEL
class ClientDocument extends Model
{
use HasFactory;
protected $connection = 'mysql';
protected $fillable = ['filename'];
protected $locale;
public function __construct() {
// SET THE LANGUAGE
if ( auth()->user() ) {
$this->locale = auth()->user()->locale;
} else {
$this->locale = 'en';
}
}
}
FACTORY
class ClientDocumentFactory extends Factory
{
public function definition()
{
$user = User::factory()->create();
$client = $user->createNewClientFile();
return [
'filename' => $this->faker->lexify('????????'),
];
}
}
MIGRATION
class CreateClientDocumentsTable extends Migration
{
public function up()
{
Schema::create('client_documents', function (Blueprint $table) {
$table->id();
$table->string('filename');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('client_documents');
}
}