mill-hill-automation / laravel-model-watch
添加了一个模型:watch 命令来轮询数据库以查找对 Eloquent 模型的更改
1.0.1
2023-09-14 12:34 UTC
Requires
- php: ^8.0
- illuminate/contracts: ^9.0 | ^10.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-09-09 15:00:51 UTC
README
添加了一个 artisan model:watch 命令,通过轮询数据库来监视 Eloquent 模型的更改。
用例 & 示例
我创建这个包是为了逆向工程一个封闭的课程系统,使用一个设置有模型从目标数据库读取的 Laravel 项目,它允许我了解系统在(例如)分配支付到发票时做了什么。
上面使用的 ModelWatchCollection 如此简单
<?php
namespace App\Collections\ModelWatch;
use App\Models\Site;
use App\Models\User;
use Mha\LaravelModelWatch\Collections\BaseWatchCollection;
use Illuminate\Support\Collection;
class ExampleWatchCollection extends BaseWatchCollection
{
public function getModels(): Collection
{
$user = User::find(9, ['id', 'name', 'email']);
$site = Site::find(44, ['id', 'name', 'slug', 'url']);
return collect([$user, $site]);
}
}
安装
您可以通过 composer 安装此包
composer require mill-hill-automation/laravel-model-watch
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="laravel-model-watch-config"
这是已发布配置文件的内容
return [ 'collections' => [ 'default' => \Mha\LaravelModelWatch\Collections\ExampleWatchUsers::class, ], ];
使用方法
监视单个模型
通过模型类和 ID 调用 artisan model:watch 命令,在您的控制台中以表格形式显示当前值。任何检测到字段更改时,都会添加新列。
artisan model:watch App/Models/Contact 2 --fields=name --fields=email
监视动态或多个模型
通过额外的步骤,您可以监视多个模型或动态选择要监视的模型。
您甚至可以查询尚未存在的模型,当它们存在时它们将显示在屏幕上。
为此,创建一个继承自 Mha\LaravelModelWatch\Collections\BaseWatchCollection 的集合,并实现 getModels() 方法,它返回要监视的模型集合。
<?php namespace App\Collections\ModelWatch; use App\Models\User; use Mha\LaravelModelWatch\Collections\BaseWatchCollection; use Illuminate\Support\Collection; class FirstUsersComments extends BaseWatchCollection { /** * Return the user with an ID of 1 and any of their posts. **/ public function getModels(): Collection { $models = new Collection; $user = User::find(1); $models[] = $user; $models->push( ...$user->comments() ) return $models; } }
测试
composer test
变更日志
请参阅 CHANGELOG 以获取有关最近更改的更多信息。
贡献
欢迎提出问题和拉取请求,尤其是带有测试的 :)
待办事项/愿望清单/想法
- 在命令中有一个提示,输入要添加到输出的事件,以帮助追踪。
鸣谢
许可
MIT 许可证(MIT)。请参阅 许可文件 以获取更多信息。
