Installation Add the package via Composer:
composer require dag-io/alice-extra-bundle
Register the bundle in config/app.php under providers:
Dag\AliceExtraBundle\AliceExtraBundle::class,
Basic Usage The package extends Laravel's Faker and Alice capabilities. Start by publishing the config (if needed) and reviewing the default providers:
php artisan vendor:publish --provider="Dag\AliceExtraBundle\AliceExtraBundle" --tag="config"
First Use Case: Custom Faker Providers Extend Faker with custom providers (e.g., for domain-specific data):
use Dag\AliceExtraBundle\Faker\Provider\CustomProvider;
Faker::addProvider(new CustomProvider($faker));
Define Fixtures Use Alice's YAML/JSON format to define complex relationships:
# database/fixtures/users.yml
App\Entity\User:
user_{1..10}:
email: '<email>'
roles: '<randomElement($roles)>'
posts: '@post*'
Load Fixtures Load fixtures via a custom Artisan command or directly in a test:
use Dag\AliceExtraBundle\Loader\Loader;
$loader = new Loader();
$loader->loadFromYaml(file_get_contents(__DIR__.'/fixtures/users.yml'));
Dynamic Data Generation Leverage Faker's custom providers for domain-specific logic:
// Extend Faker for a "Company" provider
Faker::addProvider(new CompanyProvider($faker));
$company = $faker->companyName; // Uses custom logic
AliceExtraEvents::POST_LOAD).DB::transaction(function () use ($loader) {
$loader->loadFromYaml($fixtures);
});
$user = User::factory()->create(['email' => $faker->unique()->safeEmail]);
Provider Conflicts
class CustomProvider extends \Faker\Provider\Base {
public static function register() { /* ... */ }
}
Circular References
User references Post, which references User). Use @post? (optional) or @post* (many-to-many) sparingly.Database Constraints
email) may fail. Use Faker's unique() or unique()->safeEmail:
email: '<unique()->safeEmail>'
ALICE_DEBUG=true in .env to log fixture loading details:
ALICE_DEBUG=true
--validate flag for YAML/JSON syntax checks:
php artisan alice:load --validate database/fixtures/users.yml
Custom Loaders
Extend Dag\AliceExtraBundle\Loader\Loader to add pre/post-processing:
class CustomLoader extends Loader {
protected function postLoad(array $objects) {
// Modify objects after loading
}
}
Event Listeners
Subscribe to Alice events (e.g., AliceExtraEvents::PRE_LOAD) to inject logic:
Event::listen(AliceExtraEvents::PRE_LOAD, function ($event) {
$event->setFixtures($this->modifyFixtures($event->getFixtures()));
});
Faker Extensions
Add domain-specific providers to config/alice_extra.php:
'faker_providers' => [
Dag\AliceExtraBundle\Faker\Provider\CustomProvider::class,
],
$loader->setBatchSize(100); // Load 100 objects at a time
How can I help you explore Laravel packages today?