naoray/eloquent-model-analyzer
Composer 安装命令:
composer require naoray/eloquent-model-analyzer
包简介
Helpful methods to gain more information about eloquent model classes.
README 文档
README
Analyzing an Eloquent Model for its relations and columns can be overwhelming. This little library aims to make it as simple as possible.
You probably wonder why you would ever need to analyze your Models at runtime?! All scenarios I can think of are related to analyzing the codebase to generate some code bits. Here are some scenarios where this might come in handy:
- automatically create factories for your models as shown in laravel-prefill-factory or factory-generator
- it could be use to create something like the
tracecommand in laravel-shift/blueprint
Install
composer require naoray/eloquent-model-analyzer
Usage
Getting all relations of a Model
There are three different strategies for getting all relation methods of an Eloquent Model:
- checking the return types of the methods
- extracting the return types from the doc method
- call the method directly and check the instance of what is returned
// User.php class User extends Model { public function parent() { return $this->belongsTo(self::class); } public function posts() { return $this->hasMany(Post::class, 'user_id'); } } // get relations // type of $columns is \Illuminate\Support\Collection $relations = Analyzer::relations(User::class); // get the first relation $relation = $relations->first(); // all relations implement the Arrayable interface $relation->toArray(); // [ // 'relatedClass' => User::class, // 'type' => \Illuminate\Database\Eloquent\Relations\BelongsTo::class, // 'foreignKey' => 'parent_id', // 'ownerKey' => 'id', // 'methodName' => 'parent', // ]
The RelationMethod Class forwards all method calls which aren't present on the class directly to the underlying ReflectionMethod class.
Getting all Columns of a Model
// CreateUserTable.php public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->json('bio')->nullable(); }); } // get columns // type of $columns is \Illuminate\Support\Collection $columns = Analyzer::columns(User::class); // get a single column by column name $column = $columns->get('name'); // all columns implement the Arrayable interface $column->toArray(); // [ // 'name' => 'name', // 'type' => \Doctrine\DBAL\Types\StringType::class, // 'unsigned' => false, // 'unique' => false, // 'isForeignKey' => false, // 'nullable' => false, // 'autoincrement' => false, // ]
The Column class forwards all method calls which aren't present on the class directly to the underlying DBAL\Schema\Column Class.
Testing
Run the tests with:
vendor/bin/phpunit
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security-related issues, please email krishan.koenig@googlemail.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
naoray/eloquent-model-analyzer 适用场景与选型建议
naoray/eloquent-model-analyzer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 434.77k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2020 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 naoray/eloquent-model-analyzer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 naoray/eloquent-model-analyzer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 434.77k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 28
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-04-05