ikabalzam/laravel-xray
Composer 安装命令:
composer require ikabalzam/laravel-xray
包简介
Static analysis for Eloquent column references — finds invalid database column names in your Laravel code before they hit production.
README 文档
README
Static analysis for Eloquent column references. Finds invalid database column names in your Laravel code before they hit production.
Xray uses PHP-Parser to build an AST of your codebase, then traces every ->where('column'), ->orderBy('column'), ->pluck('column') (and 30+ other methods) back to its database table and validates the column actually exists.
The Problem
// This will blow up at runtime — 'stauts' doesn't exist User::where('stauts', 'active')->get(); // This passes all tests until a specific code path runs $query->orderBy('craeted_at'); // Renamed a column in a migration but missed a reference Invoice::where('total', '>', 1000); // was renamed to 'total_in_cents'
These bugs survive code review, pass your test suite, and explode in production. Xray catches them statically — no HTTP requests, no test data, no runtime needed.
Installation
composer require ikabalzam/laravel-xray --dev
Laravel auto-discovers the service provider. No manual registration needed.
Usage
# Full audit php artisan xray:audit # Audit a specific table php artisan xray:audit --table=users # Show suggested fixes for typos php artisan xray:audit --fix # JSON output (for CI pipelines) php artisan xray:audit --json # Show unresolved references (dynamic class/table names) php artisan xray:audit --show-unresolved # Scan a specific directory php artisan xray:audit --path=app/Services
What It Catches
- Typos:
'stauts'instead of'status'(with Levenshtein suggestions) - Renamed columns: References to columns that no longer exist after a migration
- Wrong table: Column exists but on a different table than the one being queried
- Copy-paste errors: Column from one model accidentally used in another's query
What It Understands
Xray isn't a dumb regex grep. It performs deep AST analysis:
- Method chains:
User::where()->orderBy()->get()— traces the chain back toUser - Relationships:
$this->posts()->where('title', ...)— resolvesposts()to thepoststable - Closures:
->whereHas('posts', fn($q) => $q->where('title', ...))— resolves through closure context - Nested closures: Multiple closure levels deep
- Variable tracking:
$query = User::query(); $query->where('name', ...)— traces variable assignments - Self-referencing:
$q = $q->where(...)— traces back to the original assignment - Static calls:
User::where(),self::where(),static::where(),DB::table('users') - Collection detection: Won't flag
$users->where('name', ...)— it knows that'sCollection::where(), notBuilder::where() - Resources:
$this->statusin a Resource file resolves to the underlying model - Scopes:
scopeActive($query)— knows$queryis a Builder for this model - Raw SQL: Extracts columns from
selectRaw('COUNT(id) as total') - SQL aliases: Won't flag columns defined with
AS aliaselsewhere in the file - Type hints:
function foo(Collection $items)— knows to skip Collection parameters @audit-skip: Opt out of specific lines, methods, or entire classes
Suppressing False Positives
// Inline ->where('dynamic_column', $value) // @audit-skip // Line above // @audit-skip — this column exists in a dynamic view ->where('computed_field', $value) // Entire method /** @audit-skip This method uses a legacy table not in the main schema */ public function legacyQuery() { // All column references in this method are skipped }
Configuration
php artisan vendor:publish --tag=xray-config
// config/xray.php return [ // Default scan path 'path' => app_path(), // Where your models live 'models_path' => app_path('Models'), // Extra column patterns to ignore (regex) 'ignored_column_patterns' => [ '/^cached_/', // your custom virtual attributes ], // Extra methods to treat as non-relationship during chain walking 'non_relation_methods' => [ 'applyFilters', // your custom Builder macros ], // Extra Collection-only methods (NEVER add Builder methods here!) 'collection_only_methods' => [], ];
CI Integration
Xray returns exit code 1 when issues are found, making it perfect for CI:
# GitHub Actions - name: Audit column references run: php artisan xray:audit --json
# GitLab CI xray: script: php artisan xray:audit allow_failure: false
How It Works
Xray is built on nikic/php-parser and performs multi-pass analysis:
- Schema loading — Reads your live database schema (tables + columns) and scans
app/Modelsto build a model-to-table map with relationship metadata - AST parsing — Parses each PHP file into an Abstract Syntax Tree
- Context detection — Identifies model imports, class declarations,
@mixinannotations - Column extraction — Finds all method calls that accept column arguments
- Table resolution — Traces each column reference back to its database table using four strategies:
- Chain resolution: Walk method chains backward (
User::where()->orderBy()) - Closure resolution: Analyze closure parent context (
whereHas,where(fn),with) - Variable resolution: Track variable assignments and type hints
- Cross-resolver mediation: Coordinate between strategies for complex patterns
- Chain resolution: Walk method chains backward (
- Validation — Check each column against the resolved table's actual schema
Requirements
- PHP 8.1+
- Laravel 10, 11, or 12
- A running database connection (Xray reads schema metadata via
Schema::getColumnListing())
License
MIT License. See LICENSE for details.
ikabalzam/laravel-xray 适用场景与选型建议
ikabalzam/laravel-xray 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 230 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「schema」 「Audit」 「laravel」 「lint」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ikabalzam/laravel-xray 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ikabalzam/laravel-xray 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ikabalzam/laravel-xray 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Log adding/updating/deleting of elements
Doctrine ORM provider for the auditor audit-log library.
Store your language lines in the database, yaml or other sources
Extension for Opis JSON Schema
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
统计信息
- 总下载量: 230
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-08