Skip to main content

Health Check Setup

documentation spatie/laravel-health

Installation

composer require spatie/laravel-health
php artisan vendor:publish --tag="health-config"
php artisan vendor:publish --tag="health-migrations"

Configuration

// in app/Service/HealthCheckService.php

use Illuminate\Support\ServiceProvider;
use Spatie\Health\Checks\Checks\DatabaseCheck;
use Spatie\Health\Checks\Checks\DebugModeCheck;
use Spatie\Health\Checks\Checks\EnvironmentCheck;
use Spatie\Health\Checks\Checks\UsedDiskSpaceCheck;
use Spatie\Health\Facades\Health;

class HealthCheckService extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Health::checks([
            // https://spatie.be/docs/laravel-health/v1/available-checks/environment
            EnvironmentCheck::new()->unless($this->app->environment('local')),
            // https://spatie.be/docs/laravel-health/v1/available-checks/debug-mode
            DebugModeCheck::new()->unless($this->app->environment('local')),
            // https://spatie.be/docs/laravel-health/v1/available-checks/db-connection
            DatabaseCheck::new(),
            // https://spatie.be/docs/laravel-health/v1/available-checks/used-disk-space
            UsedDiskSpaceCheck::new(),
            // https://spatie.be/docs/laravel-health/v1/available-checks/queue
            // https://spatie.be/docs/laravel-health/v1/available-checks/schedule
        ]);
    }
}
// in routes/web.php

use Spatie\Health\Http\Controllers\HealthCheckResultsController;

Route::get('/up', HealthCheckResultsController::class);
// in routes/console.php
use Illuminate\Support\Facades\Schedule;

Schedule::command(\Spatie\Health\Commands\RunHealthChecksCommand::class)->everyMinute();

Custom Health Check Model

// in app/Model/HealthCheckResult.php

use Illuminate\Database\Eloquent\Attributes\Table;
use Spatie\Health\Models\HealthCheckResultHistoryItem;

#[Table(name: 'HEALTH_CHECK')]
class HealthCheckResult extends HealthCheckResultHistoryItem
{
    //
}
// in config/health.php

return [
    'result_stores' => [
        EloquentHealthResultStore::class => [
            'model' => HealthCheckResult::class,
        ],
    ],
];