indy2kro / laravel-validate-models
Validate Eloquent models against the database schema (columns, casts, fillables, relations).
0.0.4
2025-09-12 11:52 UTC
Requires
- php: ^8.1
- illuminate/console: ^10.0|^11.0|^12.0
- illuminate/database: ^10.0|^11.0|^12.0
- illuminate/filesystem: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- driftingly/rector-laravel: ^1.2
- friendsofphp/php-cs-fixer: ^3.64
- larastan/larastan: ^2.11
- orchestra/testbench: ^8.0|^9.0
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^10.0
- rector/rector: ^1.2
Suggests
- doctrine/dbal: Enable robust, cross-driver nullability checks for annotations
README
Validate your Eloquent models against the database schema. Checks columns, casts, fillable fields, and relations โ catch mismatches early in CI before they break production.
โจ Features
- ๐ Scans all models in app/Models (configurable paths/namespaces)
- โ
Validates:
- Table existence
- Column type vs. model cast (including enums + custom casters)
- Fillable fields vs. table columns
- Relations resolve without error
- โ๏ธ Configurable via config/validate-models.php
- ๐ฆ Works with Laravel 10+ / PHP 8.1+
- ๐งช Fully tested with Orchestra Testbench
- ๐ง Ships with CI tooling (PHP-CS-Fixer, PHPStan, Rector, PHPUnit)
๐ฆ Installation
composer require indy2kro/laravel-validate-models --dev
Publish the config (optional):
php artisan vendor:publish --tag=validate-models-config
โ๏ธ Configuration
config/validate-models.php
:
return [
'models_paths' => [base_path('app/Models')],
'models_namespaces' => ['App\\Models'],
'connection' => null,
'checks' => [
'columns' => true,
'casts' => true,
'fillable' => true,
'relations' => true,
'annotations'=> true,
],
'annotations' => [
'columns_only' => true, // only check @property names that exist as columns
'check_casts' => true, // also compare annotation types vs model casts
'check_nullability' => true, // enable/disable nullable enforcement
'ignore' => [ // names to ignore
],
'aliases' => [ // map short names used in @property tags to FQCNs
],
],
'ignore' => [
'casts' => [],
'fillable' => [],
'columns' => [],
'relations' => [],
],
'fail_on_warnings' => true,
'type_map' => [
'*' => [
'integer' => ['int','integer','bigint','smallint','tinyint'],
'string' => ['string','varchar','char','text','enum','set'],
'boolean' => ['bool','boolean','tinyint'],
'float' => ['float','double','decimal'],
'decimal' => ['decimal','numeric'],
'datetime' => ['datetime','timestamp'],
'json' => ['json','jsonb','array'],
'array' => ['json','jsonb','array'],
],
],
];
๐ Usage
Run the validation:
php artisan validate:models
Options:
Option Description
--path Override model paths (default: app/Models)
--namespace Override model namespaces (default: App\\Models)
--connection Use a specific DB connection
--no-columns Skip column checks
--no-casts Skip cast checks
--no-fillable Skip fillable checks
--no-relations Skip relation checks
--no-fail Always exit 0, even with warnings
๐งช Testing locally
composer install
composer cs # lint (php-cs-fixer dry-run)
composer cs:fix # auto-fix style
composer stan # static analysis (PHPStan)
composer rector # preview Rector refactors
composer rector:fix # apply Rector refactors
composer test # run PHPUnit
composer test:coverage