Created
September 24, 2023 18:07
-
-
Save muath-ye/ad708860432f725867a36f7425569e2e to your computer and use it in GitHub Desktop.
A Laravel helper to call a callable function conditionally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (! function_exists('when')) { | |
/** | |
* Execute the given callback function if the condition is true. | |
* | |
* @param bool $condition | |
* @param callable $function | |
* @return mixed|null | |
*/ | |
function when(bool $condition, callable $function) | |
{ | |
if ($condition) { | |
return $function(); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is an update for the function