Looking to hire Laravel developers? Try LaraJobs

laravel-alice maintained by kulturman

Description
YAML-based fixture loading for Laravel using nelmio/alice with Eloquent persistence
Last update
2026/05/02 14:05 (dev-main)
License
Links
Downloads
1

Comments
comments powered by Disqus

Laravel Alice

YAML-based fixture loading for Laravel, powered by nelmio/alice and theofidry/alice-data-fixtures.

Requirements

  • PHP 8.3+
  • Laravel 11+

Installation

composer require kulturman/laravel-alice

Publish the configuration file:

php artisan vendor:publish --tag=alice-config

Configuration

// config/alice.php

return [
    'fixtures_path' => database_path('fixtures'),
    'purge_mode' => 'delete', // delete | truncate | none
    'excluded_tables' => ['migrations', 'jobs', 'failed_jobs'],
    'providers' => [
        // Custom Faker providers:
        // App\Fixtures\Providers\MyProvider::class,
    ],
];

Writing Fixtures

Create YAML files in database/fixtures/:

# database/fixtures/companies.yaml
App\Models\Company:
    company_acme:
        name: "Acme Corp"
    company_{1..10}:
        name: "<company()>"
# database/fixtures/users.yaml
App\Models\User:
    user_admin:
        name: "Admin"
        email: "admin@example.com"
        company_id: 1
    user_{1..50}:
        name: "<firstName()> <lastName()>"
        email: "<email()>"
        company_id: "<numberBetween(1, 11)>"

For the full YAML reference, see the nelmio/alice documentation.

Usage

Artisan Command

# Load all fixtures (with purge confirmation)
php artisan alice:fixtures:load

# Append without purging
php artisan alice:fixtures:load --append

# Use truncate instead of delete
php artisan alice:fixtures:load --purge=truncate

# Load from a custom path
php artisan alice:fixtures:load --path=database/fixtures/dev

# Skip confirmation
php artisan alice:fixtures:load --no-interaction

In Tests

Two traits are available depending on how much control you need:

WithFixtures — automatic loading

Fixtures are loaded automatically before each test. Global fixtures from the configured path are always loaded, and a per-test fixture file is loaded if it exists next to the test class (e.g. OrderTest.yaml alongside OrderTest.php).

use Kulturman\LaravelAlice\Testing\WithFixtures;

class OrderTest extends TestCase
{
    use RefreshDatabase, WithFixtures;

    public function test_order_total(): void
    {
        // $this->fixtures contains all persisted models
        // Global fixtures + OrderTest.yaml (if it exists) are already loaded
    }
}
# tests/OrderTest.yaml — loaded only for OrderTest
App\Models\Order:
    order_1:
        user_id: 1
        total: 150

UseFixtures — manual loading

For fine-grained control, use UseFixtures and call loadFixtures() explicitly:

use Kulturman\LaravelAlice\Testing\UseFixtures;

class OrderTest extends TestCase
{
    use RefreshDatabase, UseFixtures;

    public function test_order_total(): void
    {
        // Load all fixtures from the configured path
        $objects = $this->loadFixtures();

        // Or load specific files
        $objects = $this->loadFixtures([
            database_path('fixtures/companies.yaml'),
            database_path('fixtures/users.yaml'),
        ]);

        // $objects is an array of persisted models indexed by fixture name
        $this->assertNotEmpty($objects);
    }
}

Custom Faker Providers

Create a provider class and register it in the config:

namespace App\Fixtures\Providers;

use Faker\Provider\Base;

class ProductProvider extends Base
{
    public function sku(): string
    {
        return 'SKU-' . $this->generator->unique()->numberBetween(10000, 99999);
    }
}
// config/alice.php
'providers' => [
    App\Fixtures\Providers\ProductProvider::class,
],

Then use it in fixtures:

App\Models\Product:
    product_{1..100}:
        sku: "<sku()>"
        name: "<productName()>"

License

MIT