msaddamkamal/model-dependency-sync
Composer 安装命令:
composer require msaddamkamal/model-dependency-sync
包简介
A Laravel package to sync model dependencies and provide insights on updates.
README 文档
README
A Laravel package designed to sync model dependencies and provide insights on updates, making it easier to manage complex data relationships within your Laravel applications.
Features
- Automatically sync related model data on updates.
- Provides insights into how model updates affect related data.
- Easy configuration and customization through a published template file.
Requirements
- PHP version 7.4 or higher.
- Laravel 8.0 or higher.
Installation
You can install the package via Composer:
composer require msaddamkamal/model-dependency-sync
This package uses Laravel's auto-discovery feature, so the service provider and facade will automatically be registered.
Customizing Model Behavior
This package allows you to define custom behavior for how your models' updates affect other parts of your application. To customize this behavior:
Publish the Customization Template
Run the following command to publish a template PHP file to your application's app/ directory:
php artisan vendor:publish --tag=model-dependency-sync
Customize Your Model Dependencies
After publishing, you'll find a new file named model_dependencies.php in your app/ directory.Here, you can define how your models are related and what actions should be taken on updates.
return [ // Example Model Class \App\Models\ExampleModel::class => [ 'listen' => ['field_to_listen_for_changes'], 'affect' => [ // Related Model Class \App\Models\RelatedModel::class => [ 'relation' => 'relationMethodOnExampleModel', 'actions.field_to_listen_for_changes' => [ 'update' => [ 'related_model_field' => function ($exampleModelInstance, $relatedModelInstance) { // Logic to handle the update return $newFieldValue; }, ], ], ], ], ], ];
This file allows you to specify which model attributes to "listen" for changes on, and define how those changes "affect" other models, including specifying custom actions to take using closures or other PHP logic.
Extending Functionality with a Custom Handler
If the default handling mechanism doesn't fully meet your requirements, you can extend the package's functionality by specifying a custom handler class in the configuration file.
Your custom handler class should extend the package's base handler class (MSaddamKamal\ModelDependencySync\BaseModelDependencyHandler) and override necessary methods to implement your custom logic.
To use your custom handler, define it in the config/model_dependencies.php file like so:
return [ 'handler' => \App\Handlers\MyCustomModelDependencyHandler::class, // Other configuration... ];
Publishing the Configuration
To publish the package configuration file to your application's config directory, run:
php artisan vendor:publish --tag=model-dependency-sync-config
Here's an example of what a custom handler might look like:
namespace App\Handlers; use MSaddamKamal\ModelDependencySync\BaseModelDependencyHandler; class MyCustomModelDependencyHandler extends BaseModelDependencyHandler { public function handleModelUpdated($model) { // Your custom logic here } }
By extending and defining a custom handler, you gain full control over how model updates are handled and can tailor the behavior to fit the specific needs of your application.
License
The Model Dependency Sync for Laravel is open-sourced software licensed under the MIT license.
msaddamkamal/model-dependency-sync 适用场景与选型建议
msaddamkamal/model-dependency-sync 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.65k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 03 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 msaddamkamal/model-dependency-sync 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 msaddamkamal/model-dependency-sync 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-14