Use laravelpolicynder extension to easily implement the message notification function of Laravel applications
1. Introduction
Notifynder provides powerful message notification management functions in a simple way: it provides a complete API for processing message notifications, for example, code libraries that store, retrieve, and organize hundreds of notifications. With mongoynder, you can "enable" the message notification feature in your Laravel project within a few minutes.
Currently, the supported databases include MySQL, S, and SQLite.
2. Installation
Use Composer to install the extension:
composer require fenos/notifynder
Then register the service provider in config/app. php:
Fenos\Notifynder\NotifynderServiceProvider::class,
And facade:
'Notifynder' => Fenos\Notifynder\Facades\Notifynder::class,
Publish the configuration file of this extension package to the config Directory:
php artisan vendor:publish --provider="Fenos\Notifynder\NotifynderServiceProvider"
Finally, run database migration to generate the corresponding data table:
php artisan migrate
3. Quick start
Create Category
Before starting to use the categorynder, we need to briefly understand the role of the term "classification" in the categorynder. classification is the subject of a message notification, which is distinguished by a unique name, each notification must be bound to a category to facilitate management and maintenance.
First, we use the Artisan command provided by javasynder to create a category:
php artisan notifynder:create:category "user.following" "{from.username} started to follow you"
This will create a new record in the notification_categories table of the database:
Function implementation
Next, determine the model to be notified. generally, we select the User model. the selected model class uses the retriable Trait:
use Fenos\Notifynder\Notifable;class User extends Model{ use Notifable;}
In this way, our model entity can process message notifications:
$user = User::find(1);$user->getNotifications($limit = null, $paginate = null, $order = 'desc');$user->getNotificationsNotRead($limit = null, $paginate = null, $order = 'desc');$user->getLastNotification();$user->countNotificationsNotRead($category = null);$user->readAllNotifications();
Note: If you do not want to use resumable Trait, you can also directly use the corresponding method on the mongoynder facade.
4. send notifications
Sending notifications is very simple:
$from_user_id = 1;$to_user_id = 2;Notifynder::category('user.following') ->from($from_user_id) ->to($to_user_id) ->url('http://laravelacademy.org/notifications') ->send();
After the notification is sent, we will retrieve the notification:
$userNotified = User::find($to_user_id);dd($userNotified->getNotificationsNotRead());
You can also send multiple notifications at a time. here we will send notifications to multiple users:
// It send a notification to all the userstry { $this->notifynder->loop($users, function(NotifynderBuilder $builder, $user) { $builder->category('sayhello') ->from(1) ->to($user->id) ->url('http://localhost') ->extra(compact('period_day')); })->send();} catch (EntityNotIterableException $e) {} catch (IterableIsEmptyException $e) {}
For more use, see the official documentation: https://github.com/fenos/Notifynder/wiki