maize-tech/laravel-prunable-fields
Composer 安装命令:
composer require maize-tech/laravel-prunable-fields
包简介
Laravel Prunable Fields
README 文档
README
This package allows you to clean model fields with an easy command. The feature is highly inspired by Laravel's Prunable core feature, and allows you to easily adapt all your existing models.
Installation
You can install the package via composer:
composer require maize-tech/laravel-prunable-fields
You can publish the config file with:
php artisan vendor:publish --tag="prunable-fields-config"
This is the contents of the published config file:
return [ /* |-------------------------------------------------------------------------- | Prunable models |-------------------------------------------------------------------------- | | Here you may specify the list of fully qualified class names of prunable | models. | All models listed here will be pruned when executing the model:prune-fields | command without passing the --model option. | */ 'models' => [ // \App\Models\User::class, ], ];
Usage
Prunable models
To use the package, simply add the Maize\PrunableFields\PrunableFields trait to all models you want to clean.
Once done, you can define the list of attributes who should be cleaned up by implementing the $prunable class property.
The array key should be the attribute name, whereas the array value should be the value you want the attribute to be updated to.
After that, implement the prunableFields method which should return an Eloquent query builder that resolves the models that should be cleaned up.
If needed, you can also override the pruningFields and prunedFields methods (which are empty by default) to execute some actions before and after the model is being updated.
Here's an example model including the PrunableFields trait:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Maize\PrunableFields\PrunableFields; class User extends Model { use PrunableFields; protected $fillable = [ 'first_name', 'last_name', 'email', ]; protected $prunable = [ 'first_name' => null, 'last_name' => null, ]; public function prunableFields(): Builder { return static::query() ->whereDate('created_at', '<=', now()->subDay()); } protected function pruningFields(): void { logger()->warning("User {$this->getKey()} is being pruned"); } protected function prunedFields(): void { logger()->warning("User {$this->getKey()} has been pruned"); } }
All you have to do now is including the model's class name in models attribute under config/prunable-fields.php:
'models' => [ \App\Models\User::class, ],
That's it! From now on, the model:prune-fields command will do all the magic.
In our example, all users created before the current day will be updated with a null value for both first_name and last_name attributes.
Mass prunable models
When using the MassPrunableFields trait all models will be updated with a raw database query.
In this case, pruningFields and prunedFields methods will not be invoked, and models will not fire the updating or updated events.
This way there is no need to retrieve all models before updating them, making the command execution way faster when working with a large number of entries.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Maize\PrunableFields\MassPrunableFields; class User extends Model { use MassPrunableFields; protected $fillable = [ 'first_name', 'last_name', 'email', ]; protected $prunable = [ 'first_name' => null, 'last_name' => null, ]; public function prunableFields(): Builder { return static::query() ->whereDate('created_at', '<=', now()->subDay()); } }
Scheduling models cleanup
The package is pretty useful when you automatize the execution of the model:prune-fields command, using Laravel's scheduling.
All you have to do is add the following instruction to the schedule method of the console kernel (usually located under the App\Console directory):
$schedule->command('model:prune-fields')->daily();
By default, when executing the model:prune-fields command the package will take all prunable models specified in models attribute under config/prunable-fields.php.
If you want to restrict the model list you want to automatically clean up, you can pass the --model option to the command:
$schedule->command('model:prune-fields', [ '--model' => [User::class], ])->daily();
Alternatively, you can clean up all models listed in your config and exclude some of them with the --execpt command option:
$schedule->command('model:prune-fields', [ '--except' => [PleaseLetMeCleanThisModelByHandsThankYou::class], ])->daily();
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
maize-tech/laravel-prunable-fields 适用场景与选型建议
maize-tech/laravel-prunable-fields 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.57k 次下载、GitHub Stars 达 41, 最近一次更新时间为 2022 年 06 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「fields」 「maize-tech」 「prunable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 maize-tech/laravel-prunable-fields 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 maize-tech/laravel-prunable-fields 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 maize-tech/laravel-prunable-fields 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Encryptable
Laravel Badges
Laravel nps
Register advanced custom fields with object oriented PHP
Laravel Legal Consent
A behavior for standard fields logic in CRUD based on kartik dynagrid and detailView
统计信息
- 总下载量: 1.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 41
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-10