dev-master
9999999-dev https://github.com/tricki/NotificationA basic user notification package for Laravel 4
MIT
The Requires
- php >=5.4.0
- illuminate/support 4.2.*
by Thomas Rickenbach
laravel notification
A basic user notification package for Laravel 4
A basic starting point for a flexible user notification system in Laravel 4., (*1)
It is easily extendable with new notification types and leaves rendering completely up to you., (*2)
This package only provides an extendable notification system without any controllers or views since they are often very use case specific., (*3)
I'm open to ideas for extending this package., (*4)
composer require tricki/laravel-notification:@dev
This will update composer.json
and install it into the vendor/
directory., (*5)
(See the Packagist website for a list of available version numbers and development releases.), (*6)
config/app.php
'providers' => [ // ... 'Tricki\Notification\NotificationServiceProvider', ],
This registers the package with Laravel and automatically creates an alias called
Notification
., (*7)
If your models are namespaced you will have to declare this in the package configuration., (*8)
Publish the package configuration using Artisan:, (*9)
php artisan config:publish tricki/laravel-notification
Set the namespace
property of the newly created app/config/packages/tricki/laravel-notification/config.php
to the namespace of your notification models., (*10)
'namespace' => '\MyApp\Models\'
php artisan migrate --package="tricki/laravel-notification"
Extend your User model with the following relationship:, (*11)
public function notifications() { return $this->hasMany('\Tricki\Notification\Models\NotificationUser'); }
You will need separate models for each type of notification. Some examples would
be PostLikedNotification
or CommentPostedNotification
., (*12)
These models define the unique behavior of each notification type like it's actions and rendering., (*13)
A minimal notification model looks like this:, (*14)
Remember to add the namespace of your notification models to this package's `config.php`. ### 2. Create a notification Notifications can be created using `Notification::create`. The function takes 5 parameters: * **$type** string The notification type (see [Define notification models](#1-define-notification-models)) * **$sender** Model The object that initiated the notification (a user, a group, a web service etc.) * **$object** Model | NULL An object that was changed (a post that has been liked). * **$users** array | Collection | User The user(s) which should receive this notification. * **$data** mixed | NULL Any additional data you want to attach. This will be serialized into the database. ### 3. Retrieving a user's notifications You can get a collection of notifications sent to a user using the `notifications` relationship, which will return a collection of your notification models. You can easily get a collection of all notifications sent to a user: ```php $user = User::find($id); $notifications = $user->notifications; ``` You can also only get read or unread notifications using the `read` and `unread` scopes respectively: ```php $readNotifications = $user->notifications()->read()->get(); $unreadNotifications = $user->notifications()->unread()->get(); ``` Since the notifications are instances of your own models you can easily have different behavior or output for each notification type. #### Example: ```php
// notifications.blade.php
This could output:, (*15)
<ul> <li>this is a post_liked notification</li> <li>this is a comment_posted notification</li> </ul>
A basic user notification package for Laravel 4
MIT
laravel notification