Download the PHP package proai/laravel-data-integrity without Composer
On this page you can find all versions of the php package proai/laravel-data-integrity. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download proai/laravel-data-integrity
More information about proai/laravel-data-integrity
Files in proai/laravel-data-integrity
Package laravel-data-integrity
Short Description Scan your Eloquent models for data integrity violations and fix them automatically.
License MIT
Homepage http://github.com/proai/laravel-data-integrity
Informations about the package laravel-data-integrity
Laravel Data Integrity
Scan your Eloquent models for data integrity violations and fix them automatically.
Concept
An audit case is a class that groups one or more integrity checks for a given model. Each check* method defines a single audit that examines every record in chunks and reports violations. Audit cases are discovered automatically from database/audits/, and violations can be fixed in place when passed --fix.
Installation
The package auto-registers itself via Laravel's package discovery. The db:audit command is available immediately after installation.
Since audits live in database/audits/ by default, you need to register the namespace in your application's composer.json so the classes can be autoloaded:
Then run composer dump-autoload.
Usage
Creating an audit case
Create a class anywhere under database/audits/ extending ProAI\DataIntegrity\AuditCase. It will be discovered automatically. Each public check* method defines one audit and returns an Audit.
Inline audits
Define checks as individual methods using the fluent builder:
The validate closure receives a single model instance and a $fail closure. Call $fail(string $reason) to report a violation. The model identifier (e.g. Order #42:) is automatically prepended to the reason in the output.
To support --fix, pass an optional fix closure as the second argument to $fail:
The fluent builder supports query(), chunkSize(), before(), and after():
Description from method name
When no description is passed to audit(), it is derived from the method name automatically:
Lifecycle hooks: before and after
Use before and after to run logic before and after each chunk is validated. The before hook is useful for preloading related data or setting up shared state, while after can be used for cleanup or post-processing:
Reusable checks with IntegrityCheck
For checks that are shared across multiple audits, create a class implementing IntegrityCheck:
Then reference it in your audit case with auditUsing():
Check classes can accept constructor arguments, making them reusable across different models and relationships. The example above shows how BelongsToExists can verify any belongsTo relationship — just pass the relation name.
For counter cache validation, a generic check class avoids duplicating the same logic:
Check classes can also define optional before($chunk), after($chunk), query(), and description() methods. Use before to batch-load data per chunk instead of querying per model:
Registering check aliases
You can register short aliases for check classes and reference them by name:
Configuration
All global settings are managed through the AuditManager class. Configure them in a service provider:
Individual audits can still override the chunk size per check via the chunkSize() method.
API
AuditManager
| Member | Description |
|---|---|
static defaultChunkSize(int $chunkSize): void |
Set the default chunk size (default: 1000) |
static discoverIn(string $path): void |
Override the audit discovery path |
static register(string $name, string $checkClass): void |
Register a named check alias |
static flush(): void |
Reset all settings to defaults |
AuditCase (abstract base class)
| Member | Description |
|---|---|
protected $model |
Eloquent model class to scan |
public function check*(): Audit |
Define a single audit (discovered via reflection) |
$this->audit(?string $description): Audit |
Start an inline audit with the fluent builder |
$this->auditUsing(string $class, array $args = []) |
Delegate to an IntegrityCheck class |
Audit (fluent builder)
| Method | Description |
|---|---|
query(Closure $callback) |
function ($query) — add constraints / eager loads |
chunkSize(int $size) |
Records per chunk (defaults to the global default set via AuditManager) |
before(Closure $callback) |
function ($chunk) — runs before each chunk |
after(Closure $callback) |
function ($chunk) — runs after each chunk |
validate(Closure $callback) |
function ($model, $fail) — validate a single model |
IntegrityCheck (interface)
Optionally add before(EloquentCollection $chunk): void, after(EloquentCollection $chunk): void, query(): Closure, and/or description(): string methods.
$fail closure
| Argument | Type | Description |
|---|---|---|
$reason |
string |
Violation message (model ID is auto-prepended in output) |
$fix |
Closure\|null |
Optional fix closure, called when --fix is passed. No arguments. |
License
MIT
All versions of laravel-data-integrity with dependencies
illuminate/console Version ^12.0|^13.0
illuminate/database Version ^12.0|^13.0
illuminate/support Version ^12.0|^13.0
symfony/finder Version ^7.0|^8.0