1. Introduction
The Notifynder provides a powerful message notification management feature in a simple way: The full API it provides can be used for various processing of message notifications, such as storing, retrieving, and organizing code libraries that handle hundreds of notifications. With Notifynder, you can "enable" the message notification feature in your Laravel project in a matter of minutes.
The databases currently supported include MySQL, Postgres, and SQLite.
2, installation
To install the extension using composer:
Composer require Fenos/notifynder
Then go to the config/app.php to register the service provider:
Fenos\notifynder\notifynderserviceprovider::class,
and façade:
' Notifynder ' => fenos\notifynder\facades\notifynder::class,
To publish the configuration file for this expansion pack to the Config directory:
PHP artisan vendor:publish--provider= "Fenos\notifynder\notifynderserviceprovider"
Finally run the database migration to generate the corresponding data table:
PHP Artisan Migrate
3. Quick Start
Create a Category
Before we start using notifynder, we need to understand the responsibility of the term "classification" in Notifynder, classification is the subject of message notification, differentiated by unique names, and has corresponding notification text, each of which is bound to a taxonomy for ease of management and maintenance.
First we use the Artisan command provided by Notifynder to create a taxonomy:
PHP artisan notifynder:create:category "user.following" "{From.username} started to follow"
This creates a new record in the Database notification_categories table:
Notification_categories
function realization
Next we determine the model to be notified, usually by selecting the user model, which is to use Notifable Trait for the selected model class:
Use fenos\notifynder\notifable;
Class User extends model{
Use notifable;
}
This allows our model entities to 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 notifable Trait, you can also directly use the notifynder façade of the corresponding method.
4, send the notice
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 ();
We will retrieve the notification after sending the notification:
$userNotified = User::find ($to _user_id);
DD ($userNotified->getnotificationsnotread ());
You can also send multiple notifications at once, where we send notifications to multiple users:
It send a notification to all the users
try {
$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) {
}