Installation
composer require simlux/laravel-bakery
Publish the config file (if needed):
php artisan vendor:publish --provider="Simlux\LaravelBakery\BakeryServiceProvider"
Basic Usage
The package provides a Bakery facade to interact with "recipes" (likely a customizable workflow or data transformation system). Start by defining a simple recipe in a migration or service provider:
use Simlux\LaravelBakery\Facades\Bakery;
// Register a recipe (example: transform user data)
Bakery::recipe('user_prep', function ($data) {
return [
'name' => strtoupper($data['name']),
'email' => strtolower($data['email']),
];
});
First Use Case Apply the recipe to input data:
$transformed = Bakery::apply('user_prep', [
'name' => 'John Doe',
'email' => 'JOHN@EXAMPLE.COM',
]);
// Returns: ['name' => 'JOHN DOE', 'email' => 'john@example.com']
Simlux\LaravelBakery\Facades\Bakery (core API).config/bakery.php (if published) for default settings.Simlux\LaravelBakery\BakeryServiceProvider for registration logic.Chaining Recipes: Define sequential transformations:
Bakery::recipe('user_flow', function ($data) {
return Bakery::apply('user_prep', $data);
})->then(function ($data) {
return ['processed' => true, ...$data];
});
Dynamic Recipes: Register recipes conditionally (e.g., based on user roles):
if (auth()->user()->isAdmin()) {
Bakery::recipe('admin_prep', fn($data) => [...]);
}
Model Observers: Use recipes to sanitize or transform data before/after model events:
use Simlux\LaravelBakery\Facades\Bakery;
class UserObserver {
public function saving(User $user) {
$user->attributes = Bakery::apply('user_prep', $user->attributes);
}
}
API Resources:
Apply recipes in toArray() or toResponse():
public function toArray($request) {
return Bakery::apply('api_response', parent::toArray($request));
}
use Simlux\LaravelBakery\Facades\Bakery;
class ProcessUsersCommand extends Command {
protected $signature = 'users:process';
public function handle() {
$users = User::all();
foreach ($users as $user) {
$user->update(Bakery::apply('user_prep', $user->toArray()));
}
}
}
Bakery::shouldReceive('apply')
->once()
->with('user_prep', ['name' => 'Test'])
->andReturn(['name' => 'TEST']);
Recipe Overwriting:
user_prep_v2).Bakery::recipes().Circular Dependencies:
Bakery::once() to cache results:
Bakery::recipe('safe_recipe', fn($data) => Bakery::once('safe_recipe', $data, fn() => [...]));
Data Mutability:
array_merge or spread operator:
return [...$data, 'new_field' => 'value'];
Log Recipes: Enable debug mode in config:
'debug' => env('BAKERY_DEBUG', false),
Logs recipe calls to storage/logs/laravel.log.
Inspect Recipes: Dump all registered recipes:
dd(Bakery::recipes());
Custom Recipe Storage:
Bakery::setStorage(app(CustomRecipeStorage::class));
Recipe Events:
event(new RecipeExecuted('user_prep', $data, $result));
Recipe Validation:
Bakery::recipe('validated_prep', function ($data) {
$validator = Validator::make($data, ['name' => 'required|string']);
if ($validator->fails()) throw new \Exception($validator->errors());
return [...];
});
Cache Recipes: Cache recipe results for immutable inputs:
$cacheKey = md5(serialize($data));
return Cache::remember($cacheKey, 60, fn() => Bakery::apply('recipe_name', $data));
Lazy Loading: Defer recipe registration until first use:
if (!Bakery::hasRecipe('lazy_recipe')) {
Bakery::recipe('lazy_recipe', fn($data) => [...]);
}
How can I help you explore Laravel packages today?