Useful to return the result of variable functions
Just see examples in your project
grep -R ' with(' .
For instance
./vendor/laravel/framework/src/Illuminate/Bus/Batch.php:
return with($this->prepareBatchedChain($job), function ($chain) {
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I was looking through the Laravel docs and was wonder when someone would reach for the with() helper in a real world example.
Examples or articles welcome.
Below is the example from the docs.
$callback = function ($value) {
return (is_numeric($value)) ? $value * 2 : 0;
};
$result = with(5, $callback);
// 10
$result = with(null, $callback);
// 0
$result = with(5, null);
// 5
It's useful for example in blade directives, like in this episode
https://laracasts.com/series/intermediate-laravel/episodes/12
Please or to participate in this conversation.