Skip to content

Instantly share code, notes, and snippets.

@Braunson
Created December 10, 2025 19:57
Show Gist options
  • Select an option

  • Save Braunson/00f0e32256e114eede9220a6f11c95da to your computer and use it in GitHub Desktop.

Select an option

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
<?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