grasmash/expander
Expander is a Laravel package for managing feature rollouts and gating functionality. It helps you define “expansions” that can be enabled per environment, user, or percentage, making it easy to ship safely, run experiments, and toggle features without redeploys.
Start by installing via Composer: composer require grasmash/expander. Create an expander instance (usually in a service or controller) and define expanders for each type you want to resolve—e.g., a UserExpander that fetches full user models from an ID. A minimal example:
use Grasmash\Expander\Expander;
$expander = new Expander();
$expander->addExpander('user', fn ($id) => User::find($id));
$payload = ['author' => ['type' => 'user', 'id' => 5]];
$expanded = $expander->expand($payload);
// Returns ['author' => User::find(5)]
First use case: transforming API request bodies that contain references (e.g., {"post": {"author": 123}}) into full hydrated objects before saving or processing.
addExpander() or batching with addExpanders().ModelExpander), API resources, config-driven lookups, or computed fields (e.g., full_name from first_name + last_name).author.posts) have their own expanders defined; the library handles recursive expansion automatically.Expander into jobs, actions, or controllers. For APIs, expand payloads before returning JSON resources.fn ($id) => Model::find($id) not fn ($id) => DB::table('users')->increment('views')). Expanding may be called multiple times during recursion.null, the field is omitted or remains null—explicitly handle missing references using ?? or fail-fast strategies.'type' field in your payload precisely (case-sensitive). Use constants or enums for types to avoid typos.DB::connection()->enableQueryLog() helps profile expansion cost).Expander to override behavior (e.g., custom handling for circular refs or ExpansionException logging).$expander->expand($payload, true) (second arg = true) to get metadata about expansion steps—useful for tracing unexpected output.How can I help you explore Laravel packages today?