定制 indy2kro/laravel-validate-models 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

indy2kro/laravel-validate-models

最新稳定版本:0.1.0

Composer 安装命令:

composer require indy2kro/laravel-validate-models

包简介

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

README 文档

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

统计信息

  • 总下载量: 71
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固