toneflix-code / approvable-notifications
一个用于处理需要用户交互的数据库通知的 Laravel 扩展包
Requires
- php: ^8.1|^8.2|^8.3
- illuminate/support: ^9.0|^10.0|^11.0
Requires (Dev)
- fakerphp/faker: ^1.23
- illuminate/contracts: ^9.0|^10.0|^11.0
- imanghafoori/php-imports-analyzer: ^1.0
- laravel/pint: ^1.15
- orchestra/testbench: ^8.2
- pestphp/pest: ^2.34
- pestphp/pest-plugin-laravel: ^2.4
README
Laravel Approvable Notifications 为您的项目增加了 Laravel 通知系统的功能,使用户能够与数据库通知进行交互。
使用场景
- 好友请求
- 访问请求
- 需要第三方用户批准或拒绝的任何内容。
安装
-
通过 composer 安装此包
composer require toneflix-code/approvable-notifications
-
发布资源(迁移和配置文件)
php artisan vendor:publish --tag=approvable-notifications
-
使用以下命令运行迁移
php artisan migrate
-
完成!
包发现
Laravel 会自动发现并发布服务提供者,但您可以打开 Laravel 配置文件 config/app.php,在安装 Laravel Fileable 之后,添加以下行。
在 $providers 数组中添加此包的服务提供者。
ToneflixCode\ApprovableNotifications\ApprovableNotificationsServiceProvider::class
将此包的 facade 添加到 $aliases 数组。
'ApprovableNotifications' => ToneflixCode\ApprovableNotifications\Facades\ApprovableNotifications::class
使用方法
SendsApprovableNotifications
特性
要能够使用 Approvable Notifications 发送通知,请将 \ToneflixCode\ApprovableNotifications\Traits\SendsApprovableNotifications
特性添加到您的模型中
namespace App\Models; use ToneflixCode\ApprovableNotifications\Traits\SendsApprovableNotifications; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use SendsApprovableNotifications; }
这就完成了,我们只需要在模型中使用这个特性!现在您的用户可以发送可批准的通知了。
注意:您可以在任何模型上使用
SendsApprovableNotifications
特性,它不一定是用户模型。
HasApprovableNotifications
特性
为了使模型能够通过 Approvable Notifications 接收通知,您还需要将 \ToneflixCode\ApprovableNotifications\Traits\HasApprovableNotifications
特性添加到您的模型中
namespace App\Models; use ToneflixCode\ApprovableNotifications\Traits\HasApprovableNotifications; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use HasApprovableNotifications; }
或者,如果您的模型发送和接收通知,您可以直接在模型中添加 ApprovableNotifiable
特性。
namespace App\Models; use ToneflixCode\ApprovableNotifications\Traits\ApprovableNotifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use ApprovableNotifiable; }
现在您的模型可以发送和接收可批准的通知。
发送通知
要发送通知,请在发送者模型上调用 sendApprovableNotification
方法,传递 recipient
模型、title
、message
、可选的 data
和可选的 actionable
模型参数。
$faker = \Faker\Factory::create(); $user = \App\Models\User::find(1); $sender =\App\Models\User::find(12); $actionable =\App\Models\User::find(5); $notif = $sender->sendApprovableNotification( recipient: $user, // The recieving model title: $faker->sentence(4), // The title of the notification message: $faker->sentence(10), // The notification text message body data: ['icon' => 'fas fa-info'], // Any extra data you would like to store, actionable: $actionable, // Any model you would like to access or reference later during retrieval );
访问通知
一旦通知存储在数据库中,您需要一种方便的方式从可通知实体中访问它们。ToneflixCode\ApprovableNotifications\Traits\HasApprovableNotifications
特性包含一个 approvableNotifications
Eloquent 关系,它返回实体的通知。要获取通知,您可以通过像其他 Eloquent 关系一样访问此方法。默认情况下,通知将按 created_at
时间戳排序,最新通知在集合开头。
$user = App\Models\User::find(1); foreach ($user->approvableNotifications as $notification) { echo $notification->title; }
如果您只想检索“未读”通知,可以使用 unreadApprovableNotifications
关系。同样,这些通知将按 created_at
时间戳排序,最新通知在集合开头。
$user = App\Models\User::find(1); foreach ($user->unreadApprovableNotifications as $notification) { echo $notification->title; }
或检索“已批准”通知
foreach ($user->approvedApprovableNotifications as $notification) { echo $notification->title; }
访问通知发送者
您可以通过通知实例上的 notifier
关系访问来获取发送通知的模型。
foreach ($user->approvableNotifications as $notification) { $sender = $notification->notifier; echo $sender->name; }
访问可执行模型
如果您在创建通知时添加了可执行模型,您还可以通过通知实例访问 actionable
关系。
foreach ($user->approvableNotifications as $notification) { $actionable = $notification->actionable; echo $actionable->title; }
访问已发送的通知
发送的通知也可以由发送者访问。ToneflixCode\ApprovableNotifications\Traits\SendsApprovableNotifications
特性包括一个 approvableSentNotifications
Eloquent 关系,它返回发送通知的实体的通知。
$user = App\Models\User::find(1); foreach ($user->approvableSentNotifications as $notification) { echo $notification->title; }
标记通知为已读
通常,当用户查看通知时,您会希望将其标记为“已读”。ToneflixCode\ApprovableNotifications\Traits\HasApprovableNotifications
特性提供了一个 markAsRead
方法,该方法将更新通知数据库记录中的 read_at
列。
$user = App\Models\User::find(1); foreach ($user->unreadApprovableNotifications as $notification) { echo $notification->markAsRead(); }
但是,您可以直接在通知集合上使用 markAsRead
方法,而不是逐个遍历每个通知。
$user->unreadApprovableNotifications->markAsRead();
您还可以使用批量更新查询来标记所有通知为已读,而无需从数据库中检索它们。
$user = App\Models\User::find(1); $user->unreadApprovableNotifications()->update(['read_at' => now()]);
标记通知为批准或拒绝
此库的主要目的是允许您批准或拒绝与您的模型相关的操作。ToneflixCode\ApprovableNotifications\Traits\HasApprovableNotifications
特性提供了 markAsApproved
和 markAsRejected
方法,这将更新通知数据库记录中的 status
列。
$user = App\Models\User::find(1); foreach ($user->approvableNotifications as $notification) { echo $notification->markAsApproved(); }
$user = App\Models\User::find(1); foreach ($user->approvableNotifications as $notification) { echo $notification->markAsRejected(); }
但是,您可以直接在通知集合上使用 markAsApproved
和 markAsRejected
方法,而不是逐个遍历每个通知。
$user->approvableNotifications->markAsApproved();
$user->approvableNotifications->markAsRejected();
删除通知
您可以删除通知,将其从表中完全删除。
$user->notifications()->delete();
事件和回调
新通知
当创建新通知时,我们触发 ToneflixCode\ApprovableNotifications\Events\ApprovableNotificationCreated
事件,您可以监听此事件并在需要时执行进一步操作,事件将包含相关的 notification
模型。
或者,您还可以在您的 SendsApprovableNotifications
实体上实现 newNotificationCallback
方法,该方法将在每次创建新通知时被调用,并提供相关的 $notification
模型作为第一个和唯一参数。
namespace App\Models; use ToneflixCode\ApprovableNotifications\Traits\SendsApprovableNotifications; use Illuminate\Foundation\Auth\User as Authenticatable; use ToneflixCode\ApprovableNotifications\Models\Notification; class User extends Authenticatable { use SendsApprovableNotifications; public function newNotificationCallback(Notification $notification) { // Perform any other actions here when a notification is created } }
通知更新
当通知被交互或更新时,我们触发 ToneflixCode\ApprovableNotifications\Events\ApprovableNotificationUpdated
事件,您可以监听此事件并在需要时执行进一步操作,事件将包含相关的 notification
模型。
或者,您还可以在您的 HasApprovableNotifications
实体上实现 approvedNotificationCallback
和 rejectedNotificationCallback
方法,这两个方法将在适当的时间根据其名称被调用,并将相关的 $notification
模型作为第一个和唯一参数提供。
namespace App\Models; use ToneflixCode\ApprovableNotifications\Traits\HasApprovableNotifications; use Illuminate\Foundation\Auth\User as Authenticatable; use ToneflixCode\ApprovableNotifications\Models\Notification; class User extends Authenticatable { use HasApprovableNotifications; public function approvedNotificationCallback(Notification $notification) { // Perform any other actions here when a notification is approved } public function rejectedNotificationCallback(Notification $notification) { // Perform any other actions here when a notification is rejected } }
异常
当您尝试向无效的模型(不使用 ToneflixCode\ApprovableNotifications\Traits\HasApprovableNotifications
特性的模型)发送通知时,库会抛出 ToneflixCode\ApprovableNotifications\Exception\InvalidRecipientExeption
异常。
测试
composer test
更改日志
有关最近更改的更多信息,请参阅 CHANGELOG。
贡献
有关详细信息,请参阅 CONTRIBUTING。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 code@toneflix.com.ng 而不是使用问题跟踪器。
致谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。