Created
December 10, 2025 19:57
-
-
Save Braunson/00f0e32256e114eede9220a6f11c95da to your computer and use it in GitHub Desktop.
Laravel log_if function with the option to return early or return anything you want
This file contains hidden or 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
| <?php | |
| if (! function_exists('log_if')) { | |
| /** | |
| * Log an error message if the given boolean is true | |
| * USAGE: log_if($someConditionIsTrue, 'error', 'An error occurred', ['details' => $details], true); | |
| */ | |
| function log_if(bool $boolean, string $type = 'info', string $message, array $context = [], mixed $forceReturn = false) | |
| { | |
| if ($boolean) { | |
| logger()->$type($message, $context); | |
| if ($forceReturn) { | |
| return (is_bool($forceReturn)) ? null : $forceReturn; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment