indy2kro/laravel-validate-models

Validate Eloquent models against the database schema (columns, casts, fillables, relations).

0.0.4 2025-09-12 11:52 UTC

This package is auto-updated.

Last update: 2025-09-18 06:59:56 UTC


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