3 min read

Extending Laravel container bindings

If you want to decorate something bound to the container in Laravel you can extend it. This allows you to pipe the value through some particular changes without having to re-bind the original value.

Notice in the example below we extend a value (22) bound to the container via the ‘twenty-two’ key and then extend it again referencing the previously bound value.

           app()->bind('twenty-two'), fn() => 22);
 
 app('twenty-two');
 // 22
 
 app()->extend('twenty-two'), fn($base) => $base * 2);
 
 app('twenty-two');
 // 44
 
 app()->extend('twenty-two'), fn($base) => $base + 6);
 // 50