narcisonunez/laravel-actionable-model
Composer 安装命令:
composer require narcisonunez/laravel-actionable-model
包简介
Allow actions in your laravel model
README 文档
README
Allow models to perform or receive actions.
Installation
You can install the package via composer:
composer require narcisonunez/laravel-actionable-model
You can publish and run the migrations with:
php artisan vendor:publish --provider="Narcisonunez\LaravelActionableModel\LaravelActionableModelServiceProvider" --tag="actionable-model-migrations" php artisan migrate
Basic Setup
Register your actions
Add your action in your AppServiceProvider
use Narcisonunez\LaravelActionableModel\Facades\ActionableActionTypes; use App\ActionTypes\KudosActionType; ActionableActionTypes::register([ 'like', 'kudos' => KudosActionType::class, 'celebrate' ]);
You can implement your own class extending ActionableTypeRecord to add more logic to your records.
Ex. icon method to get that specific action type icon.
Now, Any kudos action will have a method named icon.
Your actions will be used as dynamic method calls. See below.
Create a new Actionable Action Type (OPTIONAL)
php artisan actionable:type LikeActionType
Add aliases to your models (OPTIONAL)
Add your aliases in your AppServiceProvider
use App\Models\Cause; use App\Models\User; use Narcisonunez\LaravelActionableModel\Facades\ActionableModelAliases; ActionableModelAliases::register([ User::class => 'user', Cause::class => 'cause' ]);
Storing aliases in the database will prevent losing the reference if you move your models to another directory.
Update existing models references to use the new alias
In case you already have data in the database, after adding the aliases you can run:
php artisan actionable:update-aliases // Update all the records
If you want just to update a specific value
php artisan actionable:update-aliases --from="App\\User" --to="App\\Models\\User"
To update all your existing records.
Traits
A model that can perform actions you need to include:
// Imports class User extends Authenticatable { use ActionableModel; use CanPerformActions; ... }
The model that can receive the actions must implement CanBeActionable
... use Narcisonunez\LaravelActionableModel\Traits\ActionableModel; class Cause extends Model implements CanBeActionable { use ActionableModel; ... }
Basic Usage
Perform an action
// You will use your actions as methods call. $user->performActionOn($cause)->like(); $user->performActionOn($cause)->kudos(); $user->performActionOn($cause)->celebrate();
Check if the action was already made
// returns False or an ActionableTypeRecord $user->hasPerformedAction('like')->on($cause);
Toggle your actions
// remove if exists the action, otherwise creates a new like $user->performActionOn($cause)->toggle('like'); // OR // toggleACTIONTYPE $user->performActionOn($cause)->toggleLike(); $user->performActionOn($cause)->toggleKudos(); $user->performActionOn($cause)->toggleCelebrate();
Manually delete an action (See Toggle above)
if ($action = $user->hasPerformedAction('like')->on($cause) ) { $action->delete(); }
Get all the actions
$user->actions; // To help you out filtering your actions. You can use the actionsFilter method $user->actionsFilter()->get(); $user->actionsFilter()->latest(10); $user->actionsFilter()->given()->get(); $user->actionsFilter()->received()->get(); $user->actionsFilter()->ofType('like')->get(); $cause->actionsFilter()->by($user)->ofType('like')->get(); $cause->actionsFilter()->by($user)->ofType('like')->count();
- Available methods -
| Method | Description |
|---|---|
| get | Returns a collection of ActionableTypeRecord |
| count | Returns the number of records |
| given | Filters all the records where the current model performed the actions |
| received | Filters all the records where the current model received the actions |
| ofType | Filters by the actionType |
| by | Filters all the actions in the current model where the model passed to this method performed the action |
| latest | Get the collection sorted by the latest ones. |
Actionable Record
The methods above will return a collection of ActionableRecord.
The access the owner or the actionable models, you can do it like this:
$actionRecord = $user->actionsFilter()->ofType('like')->get()->first(); $actionRecord->owner; // The model that performed the action $actionRecord->actionable; // The model that received the action $actionRecord->action; // Action that was performed. ex. like, kudos, celebrate, etc. $actionRecord->type; // An alias to action
💖 Support the development
Do you like this project? Support it by donating
- PayPal: Donate
License
The MIT License (MIT). Please see License File for more information.
narcisonunez/laravel-actionable-model 适用场景与选型建议
narcisonunez/laravel-actionable-model 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 12, 最近一次更新时间为 2021 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「like」 「eloquent」 「favorite」 「narcisonunez」 「laravel-actionable-model」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 narcisonunez/laravel-actionable-model 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 narcisonunez/laravel-actionable-model 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 narcisonunez/laravel-actionable-model 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
a simple toolkit for social networks
Let users like things, confirm their likes via email and keep track of the count.
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-21