Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vandhatch's avatar

Helper Functions: When would you reach for the with function?

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
0 likes
6 replies
sr57's avatar

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) {
vandhatch's avatar

Thanks.

Did this and only found it used couple of times in the framework.

vandhatch's avatar

Thanks for sharing.

I watched the episode. It looks like the signature is different in the video than it is now. Wonder if it's behavior has expanded since the recording.

Appreciate the link.

vandhatch's avatar

Seems pretty niche.

...or the counter to the tap() helper function.

Please or to participate in this conversation.