Installation
composer require --dev guidocella/eloquent-populator
Ensure the package is in require-dev for testing environments.
Basic Usage
Add use GuidoCella\EloquentPopulator\Populator; to your factory.
Call Populator::guessFormatters($this->modelName()) in the definition() method:
public function definition(): array
{
return Populator::guessFormatters($this->modelName());
}
First Use Case
For a User model with columns first_name, email, and created_at, the factory will auto-generate:
[
'first_name' => $faker->firstName(),
'email' => $faker->unique()->safeEmail(),
'created_at' => $faker->dateTime(),
]
Merging Custom Attributes Override auto-generated values for specific columns:
public function definition(): array
{
return array_merge(
Populator::guessFormatters($this->modelName()),
['status' => 'active'] // Custom override
);
}
Handling Relationships
Populate BelongsTo relationships automatically:
// UserFactory.php
public function definition(): array
{
return Populator::guessFormatters($this->modelName());
}
// Auto-generates a related `Role` if defined in the User model.
Multilingual Support
If using laravel-multilingual, translatable fields generate locale-specific arrays:
// Auto-generates:
['title' => ['en' => 'Sample Title', 'it' => 'Titolo Esempio']]
Conditional Population
Use Populator::guessFormatters() in state methods:
public function admin()
{
return $this->state(fn(array $attributes) => [
...Populator::guessFormatters($this->modelName()),
'role' => 'admin',
]);
}
DatabaseTests or FeatureTests to avoid hardcoding test data:
$user = User::factory()->create(); // Auto-populated
Custom Formatters Extend the package by publishing and overriding config:
php artisan vendor:publish --provider="GuidoCella\EloquentPopulator\PopulatorServiceProvider"
Modify config/eloquent-populator.php to add custom column-to-formatter mappings.
Seeding
Combine with Laravel’s DatabaseSeeder for bulk data:
User::factory()->count(100)->create(); // Auto-populated
Column Name Ambiguity
name could map to firstName() or lastName()).definition() or add custom mappings in config.Reserved Keywords
created_at or updated_at are handled automatically, but custom timestamps may conflict.definition() if needed.Relationships Without Factories
BelongsTo relationships, but if the related model lacks a factory, it fails.Populator::skipRelationships().Performance
Populator::guessFormatters($modelName, true) for one-time generation.Inspect Guessed Formatters Temporarily log the output to verify mappings:
dd(Populator::guessFormatters($this->modelName()));
Check Database Schema
Ensure column names/types match expectations (e.g., VARCHAR vs. TEXT for text() formatter).
Custom Formatters
Extend the formatters array in config/eloquent-populator.php:
'formatters' => [
'custom_column' => fn(Faker $faker) => $faker->uuid,
],
Excluded Columns
Skip specific columns (e.g., password):
Populator::guessFormatters($this->modelName(), [], ['password']);
Custom Guesser
Implement GuidoCella\EloquentPopulator\Contracts\ColumnGuesser to add logic for niche use cases.
Event Listeners
Hook into eloquent-populator.guessing or eloquent-populator.guessed events for real-time modifications:
Event::listen('eloquent-populator.guessed', function ($model, $attributes) {
$attributes['custom_field'] = 'hardcoded';
});
Testing Mock Populator in unit tests:
$this->partialMock(Populator::class, ['guessFormatters'])
->shouldReceive('guessFormatters')
->andReturn(['key' => 'value']);
How can I help you explore Laravel packages today?