定制 devlin/laravel-model-analyzer 二次开发

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

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

devlin/laravel-model-analyzer

Composer 安装命令:

composer require devlin/laravel-model-analyzer

包简介

Analyze and validate Eloquent relationships against database schema

README 文档

README

A Laravel package that scans your Eloquent models and validates their relationships against your actual database schema. It detects missing inverse relationships, circular dependencies, missing foreign key columns, missing indexes, and more — then reports a health score for your model layer.

Demo

You can check a quick demo here

Requirements

Dependency Version
PHP ^7.4 or ^8.0
Laravel / Illuminate ^8.0
Symfony Finder ^5.0

Installation

Install via Composer:

composer require devlin/laravel-model-analyzer

Laravel's package auto-discovery registers the service provider automatically. If you have auto-discovery disabled, add the provider manually in config/app.php:

'providers' => [
    Devlin\ModelAnalyzer\ModelAnalyzerServiceProvider::class,
],

Publish the config file

php artisan vendor:publish --provider="Devlin\ModelAnalyzer\ModelAnalyzerServiceProvider"

This creates config/model-analyzer.php.

Configuration

// config/model-analyzer.php

return [
    // Directories where your Eloquent models live
    'model_paths' => [
        app_path('Models'),
        app_path(), // Laravel < 8
    ],

    // Fully-qualified class names to skip
    'excluded_models' => [
        'Illuminate\Notifications\DatabaseNotification',
    ],

    // Database tables to skip
    'excluded_tables' => [
        'migrations',
        'failed_jobs',
        'password_resets',
        'personal_access_tokens',
    ],

    // Database connection to use (null = default connection)
    'database_connection' => null,

    // When true, warnings are treated as errors (non-zero exit code)
    'strict_mode' => env('MODEL_ANALYZER_STRICT', false),

    // Point weights used to calculate the 0–100 health score
    'health_weights' => [
        'has_inverse'     => 30,
        'no_circular'     => 30,
        'column_exists'   => 20,
        'has_index'       => 10,
        'has_foreign_key' => 10,
    ],
];

Commands

model-analyzer:analyze

Runs a full analysis of all discovered models and prints a report.

php artisan model-analyzer:analyze

Options:

Option Description
--format=cli Output format: cli (default) or json
--strict Exit with code 1 if any warnings are found
--models=User,Post Analyze only the specified models (comma-separated)

Examples:

# Default CLI report
php artisan model-analyzer:analyze

# JSON output (pipe-friendly, useful in CI)
php artisan model-analyzer:analyze --format=json

# Fail CI if any warnings exist
php artisan model-analyzer:analyze --strict

# Analyze a single model
php artisan model-analyzer:analyze --models=User

Exit codes:

  • 0 — no errors (warnings are allowed unless --strict is used)
  • 1 — errors found, or warnings found with --strict

model-analyzer:health

Displays a summary health score and grouped recommendation report.

php artisan model-analyzer:health

Exit codes:

  • 0 — no errors
  • 1 — one or more errors detected

model-analyzer:list-models

Lists all Eloquent models discovered in the configured paths.

php artisan model-analyzer:list-models

Options:

Option Description
--with-relationships Show relationship count per model
--json Output as a JSON array of fully-qualified class names

Examples:

php artisan model-analyzer:list-models
php artisan model-analyzer:list-models --with-relationships
php artisan model-analyzer:list-models --json

model-analyzer:visualize

Generates a visual diagram of your model relationships as a standalone file.

php artisan model-analyzer:visualize

Options:

Option Description
--output=path Output file path (default: model-relationships.html or model-erd.html)
--models=User,Post Comma-separated list of models to include
--erd Generate an Entity Relationship Diagram instead of a force-directed graph
--format=html Output format: html (interactive, D3.js) or svg (static, embeddable)

Examples:

# Interactive HTML graph (default)
php artisan model-analyzer:visualize

# ERD with table boxes, columns, and crow's foot cardinality
php artisan model-analyzer:visualize --erd

# Static SVG — embeddable in docs, READMEs, presentations
php artisan model-analyzer:visualize --format=svg

# SVG ERD for specific models
php artisan model-analyzer:visualize --erd --format=svg --models=User,Post

# Custom output path
php artisan model-analyzer:visualize --format=svg --output=docs/models.svg

HTML format produces a standalone file with D3.js — drag nodes, zoom, hover for details.

SVG format produces a pure <svg> file with no JavaScript — lightweight, scalable, and works anywhere images are supported.

What It Detects

Issue Severity Description
Missing inverse relationship Warning e.g. User hasMany Post exists but Post belongsTo User is missing
Circular dependency Error Two models reference each other in a way that creates a loop
Missing foreign key column Error A relationship references a column that does not exist in the database
Missing index on foreign key Warning A foreign key column has no index, which can hurt query performance

Health Score

The model-analyzer:analyze and model-analyzer:health commands calculate a 0–100 health score based on the number and severity of issues found relative to the total number of relationships. A score of 100 means no issues were detected.

CI Integration

Run the analyzer in strict mode to fail your pipeline when any issue is found:

# GitHub Actions example
- name: Analyze models
  run: php artisan model-analyzer:analyze --strict

Or allow warnings but fail only on errors (the default):

- name: Analyze models
  run: php artisan model-analyzer:analyze

Running Tests

composer test

License

MIT

devlin/laravel-model-analyzer 适用场景与选型建议

devlin/laravel-model-analyzer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「linter」 「laravel」 「analyzer」 「Relationships」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 devlin/laravel-model-analyzer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 devlin/laravel-model-analyzer 我们能提供哪些服务?
定制开发 / 二次开发

基于 devlin/laravel-model-analyzer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-19