What You Can Check
Data Health can track any application-specific data problem that belongs to a persisted model. These examples use illustrative Laravel models and business rules; adapt the queries to your application’s requirements and legitimate exceptions.
The first example is a complete finding class. The remaining examples show focused detect() excerpts for separate classes in App\DataHealth, each extending Finding and implementing CanDetect. They use the same DataHealth\Finding and DataHealth\Contracts\CanDetect imports as the first example.
Contradictory fields: paid order still pending
Section titled “Contradictory fields: paid order still pending”Assume an order with a payment timestamp must no longer have the pending status. Save this complete class as app/DataHealth/PaidOrderMarkedPending.php:
<?php
declare(strict_types=1);
namespace App\DataHealth;
use App\Models\Order;use DataHealth\Attributes\Description;use DataHealth\Contracts\CanDetect;use DataHealth\Contracts\CanResolve;use DataHealth\Contracts\CanVerify;use DataHealth\Finding;
#[Description('A paid order is still marked as pending.')]class PaidOrderMarkedPending extends Finding implements CanDetect, CanVerify, CanResolve{ public static function detect(): null { Order::query() ->whereNotNull('paid_at') ->where('status', 'pending') ->eachById(fn (Order $order) => self::found($order));
return null; }
public function verify(): bool { return $this->model->paid_at === null || $this->model->status !== 'pending'; }
public function resolve(): bool { if ($this->model->paid_at === null || $this->model->status !== 'pending') { return true; }
return $this->model->update(['status' => 'paid']); }}detect() reports affected orders, verify() returns true when the problem is gone, and resolve() checks the model before correcting its status. Reuse an existing domain action when it owns this transition. Resolution runs when requested; automatic resolution requires a separate opt-in.
php artisan health:detect PaidOrderMarkedPending -nThe Quick Start walks through recording, verifying, and resolving this finding.
Missing relationships: shipped order without a shipment
Section titled “Missing relationships: shipped order without a shipment”Assume every shipped order must have at least one shipment record. Use this excerpt in ShippedOrderWithoutShipment:
use App\Models\Order;
public static function detect(): null{ Order::query() ->where('status', 'shipped') ->whereDoesntHave('shipments') ->eachById(fn (Order $order) => self::found($order));
return null;}An operator must establish whether shipment data is missing or the order status is wrong before correcting it, so this example omits CanResolve.
Incorrect calculated values: invoice total differs from its lines
Section titled “Incorrect calculated values: invoice total differs from its lines”Assume an invoice’s total is exactly the sum of its line totals, including any adjustments represented as lines. Amounts use integer minor units, and Invoice::total_minor is cast to an integer. Use this excerpt in InvoiceTotalMismatch:
use App\Models\Invoice;
public static function detect(): null{ Invoice::query() ->withSum('lines', 'total_minor') ->eachById(function (Invoice $invoice): void { $expectedTotal = (int) $invoice->lines_sum_total_minor;
if ($invoice->total_minor !== $expectedTotal) { self::found($invoice); } });
return null;}A resolver can call the application’s existing total-recalculation action when line totals are authoritative and the invoice is still editable. See Resolving Findings for implementing the correction.
Duplicates: customers with the same external identity
Section titled “Duplicates: customers with the same external identity”Assume a normalized external customer ID must be unique within an account. This excerpt for DuplicateExternalCustomer reports each duplicate after the customer with the lowest numeric ID:
use App\Models\Customer;
public static function detect(): null{ Customer::query() ->whereNotNull('external_id') ->eachById(function (Customer $customer): void { $duplicateExists = Customer::query() ->where('account_id', $customer->account_id) ->where('external_id', $customer->external_id) ->where('id', '<', $customer->id) ->exists();
if ($duplicateExists) { self::found($customer); } });
return null;}The lowest ID only prevents reporting every member of a duplicate group; an operator must decide which customer to keep and how to merge their related data before resolution can be implemented.
Cross-tenant inconsistencies: invoice linked to another account’s customer
Section titled “Cross-tenant inconsistencies: invoice linked to another account’s customer”Assume an invoice and its customer must belong to the same account. Use this excerpt in InvoiceCustomerAccountMismatch:
use App\Models\Invoice;
public static function detect(): null{ Invoice::query() ->whereHas('customer', fn ($query) => $query ->whereColumn('customers.account_id', '!=', 'invoices.account_id')) ->eachById(fn (Invoice $invoice) => self::found($invoice));
return null;}Run this check with authorized visibility into both accounts: tenant scopes can hide mismatched relationships. An operator must identify the correct customer or invoice owner before changing either link.
Time-based problems: an import has stopped progressing
Section titled “Time-based problems: an import has stopped progressing”Assume a running import must write a non-null heartbeat at least every 30 minutes. Use this excerpt in StalledImport:
use App\Models\ImportRun;
public static function detect(): null{ ImportRun::query() ->where('status', 'running') ->where('heartbeat_at', '<', now()->subMinutes(30)) ->eachById(fn (ImportRun $run) => self::found($run));
return null;}Use Scheduling and Queues to run the check periodically. A resolver may invoke an existing recovery action if it safely coordinates with the worker; otherwise an operator must investigate before retrying the import.
External reconciliation: subscription differs from the billing provider
Section titled “External reconciliation: subscription differs from the billing provider”Assume local and remote subscription statuses use the same values and should agree. The application-owned BillingProvider::findSubscription() returns a validated object with a string status and throws \RuntimeException when the remote state cannot be retrieved or validated. Use this excerpt in ExternalSubscriptionStatusMismatch:
use App\Models\Subscription;use App\Services\BillingProvider;use DataHealth\Facades\CheckCursor;
public static function detect(): null{ $billing = app(BillingProvider::class);
$subscriptions = CheckCursor::next( Subscription::query()->whereNotNull('provider_id'), self::class, 25, );
foreach ($subscriptions as $subscription) { try { $remote = $billing->findSubscription($subscription->provider_id); } catch (\RuntimeException $exception) { report($exception);
continue; }
if ($remote->status !== $subscription->status) { self::found($subscription); } }
return null;}Each invocation selects up to 25 subscriptions, continuing through numeric id values on subsequent runs. Failed requests are reported separately and produce no finding. The cursor advances on selection, so failed checks wait until the scan wraps; see Large Datasets for batching and retry behavior.
A resolver can invoke the application’s synchronization action when the billing provider is established as authoritative; otherwise an operator must decide which system to correct.
Several problems on one model: missing required documents
Section titled “Several problems on one model: missing required documents”Assume every customer requires both an identity document and a tax certificate. This excerpt for MissingRequiredDocument records each missing document type as a separate occurrence:
use App\Models\Customer;
public static function detect(): null{ Customer::query() ->with('documents') ->eachById(function (Customer $customer): void { foreach (['identity-document', 'tax-certificate'] as $type) { if (! $customer->documents->contains('type', $type)) { self::found($customer, ['document_type' => $type]); } } });
return null;}The stable document_type context distinguishes the two occurrences. Repeated detection refreshes each existing record, and verification can recheck the document type stored in $this->context. See Context and Verifying Findings. This example omits resolution because the customer must supply the missing documents.
If an import or application workflow already knows a document is missing, it can report the same finding directly without running a scan:
MissingRequiredDocument::found($customer, [ 'document_type' => 'tax-certificate',]);