tomeasterbrook / livewire-fakeable
Fill your Livewire component state with realistic fake data during local development.
Package info
github.com/TomEasterbrook/livewire-fakeable
pkg:composer/tomeasterbrook/livewire-fakeable
Fund package maintenance!
Requires
- php: ^8.1
- illuminate/contracts: ^10.0|^11.0|^12.0|^13.0
- livewire/livewire: ^4.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- spatie/laravel-ray: ^1.35
README
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 »
Installation
composer require tomeasterbrook/livewire-fakeable
The 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).
Usage
Explicit formatters
Annotate public properties with #[Fakeable] and a Faker formatter name. Livewire Fakeable runs after mount and only fills properties that are still null, '', or [].
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, or fix a seed for stable reloads (screenshots, demos):
#[Fakeable('sentence', nbWords: 3)] public string $title = ''; #[Fakeable('name', seed: 42)] public string $name = '';
Bare #[Fakeable] — automatic inference
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 case
Property 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 |
Enums
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;
Array shapes
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'],
]
State classes
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 Form objects
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; }
HasFakeable trait
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 & indicator
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.
Safety
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.
Configuration
// config/fakeable.php return [ 'enabled' => env('FAKEABLE_ENABLED', true), 'allowed_hosts' => [ '*.test', '*.dev', 'localhost', ], 'locale' => 'en_US', 'show_indicator' => true, ];
Testing
composer test
Changelog
Please see CHANGELOG.
Contributing
Please see CONTRIBUTING.
Security
Please see our security policy.
Credits
License
Livewire Fakeable is open-sourced software licensed under the MIT license.
