Package Data | |
---|---|
Maintainer Username: | tricki |
Maintainer Contact: | thomasrickenbach@gmail.com (Thomas Rickenbach) |
Package Create Date: | 2015-01-24 |
Package Last Update: | 2015-12-23 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-02 03:02:03 |
Package Statistics | |
---|---|
Total Downloads: | 290 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 27 |
Total Watchers: | 8 |
Total Forks: | 8 |
Total Open Issues: | 3 |
A basic starting point for a flexible user notification system in Laravel 4.
It is easily extendable with new notification types and leaves rendering completely up to you.
This package only provides an extendable notification system without any controllers or views since they are often very use case specific.
I'm open to ideas for extending this package.
composer require tricki/laravel-notification:@dev
This will update composer.json
and install it into the vendor/
directory.
(See the Packagist website for a list of available version numbers and development releases.)
config/app.php
'providers' => [
// ...
'Tricki\Notification\NotificationServiceProvider',
],
This registers the package with Laravel and automatically creates an alias called
Notification
.
If your models are namespaced you will have to declare this in the package configuration.
Publish the package configuration using Artisan:
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.
'namespace' => '\MyApp\Models\'
php artisan migrate --package="tricki/laravel-notification"
Extend your User model with the following relationship:
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
.
These models define the unique behavior of each notification type like it's actions and rendering.
A minimal notification model looks like this:
<?php
class PostLikedNotification extends \Tricki\Notification\Models\Notification
{
public static $type = 'post_liked';
}
The type will be saved in the database to differentiate between different types. The class name must be the CamelCase version of this type and end with "Notification".
Remember to add the namespace of your notification models to this package's
config.php
.
Notifications can be created using Notification::create
.
The function takes 5 parameters:
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:
$user = User::find($id);
$notifications = $user->notifications;
You can also only get read or unread notifications using the read
and unread
scopes respectively:
$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.
<?php
class PostLikedNotification extends \Tricki\Notification\Models\Notification
{
public static $type = 'post_liked';
public function render()
{
return 'this is a post_liked notification';
}
}
class CommentPostedNotification extends \Tricki\Notification\Models\Notification
{
public static $type = 'comment_posted';
public function render()
{
return 'this is a comment_posted notification';
}
}
?>
// notifications.blade.php
<ul>
@foreach($user->notifications as $notification)
<li>{{ $notification->render() }}</li>
@endforeach
</ul>
This could output:
<ul>
<li>this is a post_liked notification</li>
<li>this is a comment_posted notification</li>
</ul>