2017 © Pedro Peláez
 

library laravel-notification

A basic user notification package for Laravel 4

image

tricki/laravel-notification

A basic user notification package for Laravel 4

  • Wednesday, December 23, 2015
  • by tricki
  • Repository
  • 7 Watchers
  • 27 Stars
  • 201 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 9 Forks
  • 3 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Laravel 4 Notification

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)

Installation

1. Install with Composer

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)

2. Add to Providers in config/app.php

    'providers' => [
        // ...
        'Tricki\Notification\NotificationServiceProvider',
    ],

This registers the package with Laravel and automatically creates an alias called Notification., (*7)

3. Publishing config

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)

Example

'namespace' => '\MyApp\Models\'

4. Executing migration

php artisan migrate --package="tricki/laravel-notification"

5. Adding relationship to User

Extend your User model with the following relationship:, (*11)


public function notifications() { return $this->hasMany('\Tricki\Notification\Models\NotificationUser'); }

Usage

1. Define notification models

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



    @foreach($user->notifications as $notification)
  • {{ $notification->render() }}
  • @endforeach

This could output:, (*15)

<ul>
    <li>this is a post_liked notification</li>
    <li>this is a comment_posted notification</li>
</ul>

The Versions

23/12 2015

dev-master

9999999-dev https://github.com/tricki/Notification

A basic user notification package for Laravel 4

  Sources   Download

MIT

The Requires

 

by Thomas Rickenbach

laravel notification