Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Health Laravel Package

spatie/laravel-health

Monitor your Laravel app’s health by registering checks (disk space, etc.) with warning/fail thresholds. Get notified via mail or Slack when checks degrade, and extend with custom checks for proactive alerting.

View on GitHub
Deep Wiki
Context7

title: Conditionally running or modifying checks weight: 5

This package provides methods to run certain checks only when specified conditions are met.

If you would like to conditionally run a check, you can use the if and unless methods. For more control, you can also use callables. They are evaluated every time a health check is run.

use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\DebugModeCheck;
use Spatie\Health\Checks\Checks\RedisCheck;

Health::checks([
    DebugModeCheck::new()->unless(app()->environment('local')),
    RedisCheck::new()->if(fn () => app(SomeHeavyService::class)->shouldCheckHealth()),
]);

Important!
Be cautious when conditionally running or modifying checks. By default, skipped checks are considered failures, which may impact the overall health status. You can adjust this behavior by setting the configuration option treat_skipped_as_failure to false in the health config file.

Custom condition methods

You may find yourself repeating conditions for multiple checks. To avoid that, you can register a Laravel macro on a check with a custom condition method.

use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Check;
use Spatie\Health\Checks\Checks\DebugModeCheck;
use Spatie\Health\Checks\Checks\RedisCheck;

Check::macro('ifEnvironment', fn (string|array $envs) => $this->if(fn () => app()->environment($envs)));

Health::checks([
    DebugModeCheck::new()->ifEnvironment('production')
]);

Chaining conditions

Sometimes you need more than one condition on a check, so you may chain two or more of them simply by calling if or unless multiple times. They are evaluated in the order that you define them.

use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\DebugModeCheck;
use Spatie\Health\Checks\Checks\RedisCheck;

Health::checks([
    DebugModeCheck::new()
        ->unless(app()->environment('local'))
        ->if(fn () => app(SomeHeavyService::class)->shouldCheckHealth()),
]);

Modifying checks on a condition

You may want to slightly change check's configuration under a specific condition. You can do so using when and doUnless methods. In this example, a smaller memory limit is enforced on a local environment.

use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\RedisMemoryUsageCheck;

Health::checks([
    RedisMemoryUsageCheck::new()
        ->failWhenAboveMb(1000)
        ->when(
            app()->environment('local'), 
            fn (RedisMemoryUsageCheck $check) => $check->failWhenAboveMb(200)
        ),
]);
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport