Livewire 4. Fill empty component state with Faker while you build — after
mount, only on your machine, never overwriting values you already set.
Livewire Fakeable is a focused Laravel package with a simple idea: declare fake data next to your Livewire properties, and let a component hook apply it when it is safe. No seeding scripts scattered across mount() methods, and no guessing whether you are looking at real or dummy data.
- Install from packagist.org/packages/tomeasterbrook/livewire-fakeable »
- Report issues on github.com/TomEasterbrook/livewire-fakeable »
composer require tomeasterbrook/livewire-fakeableThe service provider is discovered automatically. Publish the config if you want to change locale, hosts, or the on-page indicator:
php artisan vendor:publish --tag="livewire-fakeable-config"Requires PHP 8.1+, Laravel 10–13, and Livewire 4 (livewire/livewire:^4.0 is required by this package).
Annotate public properties with #[Fakeable] and a Faker formatter name. Livewire Fakeable runs after mount and only fills properties that are still empty — null, '', [], or arrays where every leaf value is one of those (e.g. placeholder structures like [['name' => '', 'phone' => '']]).
use Livewire\Component;
use TomEasterbrook\LivewireFakeable\Attributes\Fakeable;
class EditProfilePage extends Component
{
#[Fakeable('name')]
public string $name = '';
#[Fakeable('safeEmail')]
public string $email = '';
#[Fakeable('paragraph')]
public string $bio = '';
}Pass arguments through to Faker using the named formatterArguments parameter, or fix a seed for stable reloads (screenshots, demos):
// Preferred: named formatterArguments
#[Fakeable('sentence', formatterArguments: [12])]
public string $title = '';
#[Fakeable('boolean', formatterArguments: [100])]
public bool $alwaysTrue = false;
#[Fakeable('randomElement', formatterArguments: [['draft', 'published', 'archived']])]
public string $status = '';
// Alternative: variadic named args still work
#[Fakeable('sentence', nbWords: 3)]
public string $subtitle = '';
#[Fakeable('name', seed: 42)]
public string $name = '';Use the attribute without a formatter and Livewire Fakeable will infer one automatically. It checks in order:
- Enum type — picks a random case from the property's enum type.
- Property name — maps common names to Faker formatters (see table below).
- PHP type — falls back to the property's type (
string→word,int→randomNumber,float→randomFloat,bool→boolean).
#[Fakeable]
public string $email = ''; // inferred → safeEmail
#[Fakeable]
public int $quantity = 0; // inferred → randomNumber
#[Fakeable]
public OrderStatus $status; // inferred → random enum caseProperty name → formatter map
| Property name(s) | Formatter |
|---|---|
name, fullName, full_name |
name |
firstName, first_name |
firstName |
lastName, last_name |
lastName |
email, emailAddress, email_address |
safeEmail |
phone, phoneNumber, phone_number |
phoneNumber |
address |
address |
street, streetAddress, street_address |
streetAddress |
city |
city |
state |
state |
country |
country |
postcode, zipCode, zip_code |
postcode |
company, companyName, company_name |
company |
title |
sentence |
description, bio |
paragraph |
summary |
sentence |
url, website |
url |
username, user_name |
userName |
PHP enums (unit, string-backed, and int-backed) are supported out of the box. A bare #[Fakeable] attribute detects the enum type and picks a random case. Seeds are supported for deterministic selection.
enum OrderStatus: string
{
case Pending = 'pending';
case Shipped = 'shipped';
case Delivered = 'delivered';
}
#[Fakeable]
public OrderStatus $status;
#[Fakeable(seed: 42)]
public OrderStatus $lockedStatus;Generate arrays of fake data by passing an array shape — keys are output keys, values are Faker formatter names. Use count to control the number of rows:
#[Fakeable(['name' => 'name', 'email' => 'safeEmail'], count: 3)]
public array $users = [];This produces an array like:
[
['name' => 'Jane Doe', 'email' => 'jane@example.com'],
['name' => 'John Smith', 'email' => 'john@example.com'],
['name' => 'Alice Brown', 'email' => 'alice@example.com'],
]Arrays pre-filled with empty placeholder structures are also replaced — as long as every leaf value is null, '', or []:
#[Fakeable(['name' => 'name', 'phone' => 'phoneNumber', 'email' => 'safeEmail'], count: 2)]
public array $references = [
['name' => '', 'phone' => '', 'email' => ''],
['name' => '', 'phone' => '', 'email' => ''],
];For a whole component, use a state class that returns an array keyed by property name, then reference it on the class:
use Faker\Generator;
class ProfileFormState
{
public function __invoke(Generator $faker): array
{
return [
'name' => $faker->name(),
'email' => $faker->safeEmail(),
];
}
}use App\FakerStates\ProfileFormState;
use Livewire\Component;
use TomEasterbrook\LivewireFakeable\Attributes\Fakeable;
#[Fakeable(ProfileFormState::class)]
class EditProfilePage extends Component
{
public string $name = '';
public string $email = '';
}You can mix class-level and property-level #[Fakeable] — property-level attributes take precedence.
Livewire Fakeable automatically resolves #[Fakeable] attributes on Livewire Form properties too. Both property-level and class-level attributes work on Form classes:
use Livewire\Form;
use TomEasterbrook\LivewireFakeable\Attributes\Fakeable;
class ProfileForm extends Form
{
#[Fakeable('name')]
public string $name = '';
#[Fakeable('safeEmail')]
public string $email = '';
}class EditProfilePage extends Component
{
public ProfileForm $form;
}You can also call fakeable() from mount() with the HasFakeable trait for programmatic control. The trait respects the same guard conditions.
use App\FakerStates\ProfileFormState;
use Livewire\Component;
use TomEasterbrook\LivewireFakeable\Concerns\HasFakeable;
class EditProfilePage extends Component
{
use HasFakeable;
public string $name = '';
public string $email = '';
public function mount(): void
{
$this->fakeable(ProfileFormState::class);
}
}Locale — set locale in config/fakeable.php (default en_US). Any Faker-supported locale works.
Indicator — when faking is active, a collapsible banner is injected before </body> so you do not confuse local data with production. Its collapsed state persists in localStorage. Disable with show_indicator in the config.
Livewire Fakeable is for local development only. Faking runs only when all of the following are true:
enabledistruein config- The app environment is
local - The request host matches at least one
allowed_hostspattern (case-insensitivefnmatchglobs —*.testmatchesmyapp.testandsub.myapp.test) Faker\Generatorexists as a class
Because APP_ENV=testing is not local, your test suite is unaffected. If any condition fails, Livewire Fakeable does nothing — no properties are touched, no banner is injected.
// config/fakeable.php
return [
'enabled' => env('FAKEABLE_ENABLED', true),
'allowed_hosts' => [
'*.test',
'*.dev',
'localhost',
],
'locale' => 'en_US',
'show_indicator' => true,
];composer testPlease see CHANGELOG.
Please see CONTRIBUTING.
Please see our security policy.
Livewire Fakeable is open-sourced software licensed under the MIT license.
