Owner models
The model that owns health checks decides who receives alert notifications. Implement HasNotifiablesForAlerts and use the UsesHealthChecks trait:
use Closure;
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Alerts\Interfaces\HasNotifiablesForAlerts;
use RoundlyConsulting\Alerts\Traits\UsesHealthChecks;
class Team extends Model implements HasNotifiablesForAlerts
{
use UsesHealthChecks;
public function forEachNotifiableForAlerts(Closure $callback): void
{
$this->members()
->where('alerts_enabled', true)
->each(fn (Member $member) => $callback($member));
}
}forEachNotifiableForAlerts() hands every recipient in the default group to the callback. Recipients are sent notifications through Laravel’s Notification facade, so give them the Notifiable trait. The trait adds:
- healthChecks() and alerts() — morphMany relations to the owner’s scheduled checks and alerts.
- monitorCheck(string $check) — shortcut for Health::for($this)->monitor($check), the fluent scheduling builder (see Scheduling checks).
- monitor(ScheduleHealthCheckData $data) — shortcut for Health::for($this)->schedule($data); createHealthCheck(…) is its positional variant.
- notifiablesForAlertGroup(string $group) — via the bundled ResolvesAlertGroups trait, it returns the default group for any name. Override it for named escalation groups (see Escalation policies).
The scheduling shortcuts delegate to Health::for($this), so they behave exactly like the facade calls — and Health::fake() records them too.
Any model can be an owner
Teams, tenants, projects or a single user — whatever should own its checks. A user can simply notify itself:
use Closure;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use RoundlyConsulting\Alerts\Interfaces\HasNotifiablesForAlerts;
use RoundlyConsulting\Alerts\Traits\UsesHealthChecks;
// A user can own checks and receive its own alerts.
class User extends Authenticatable implements HasNotifiablesForAlerts
{
use Notifiable;
use UsesHealthChecks;
public function forEachNotifiableForAlerts(Closure $callback): void
{
$callback($this);
}
}If a check fails for an owner that does not implement the interface, the run throws InvalidNotifiableForHealthCheck.
Reading alerts
Alert has open() and recovered() scopes and relations back to its owner and health check:
$team->healthChecks()->get(); // scheduled HealthCheck rows
$team->alerts()->open()->get(); // incidents still in progress
$team->alerts()->recovered()->latest('recovered_at')->first();
$alert = $team->alerts()->open()->first();
$alert->status; // Status::Failed or Status::Warning
$alert->message; // the result message that opened it
$alert->meta; // the result meta (plus 'muted' => true when muted)
$alert->escalation_level; // 0 until a policy escalates it
$alert->triggered_at; // Carbon
$alert->recovered_at; // null while open
$alert->healthCheck; // the scheduled HealthCheck row
$alert->notifiable; // the ownerShow your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.