Giter Site home page Giter Site logo

laravel-notification's Introduction

Laravel 4 Notification

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.

Installation

1. Install with Composer

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.)

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.

3. Publishing config

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.

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:

	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.

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.

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)
  • $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:

$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.

Example:

<?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>

laravel-notification's People

Contributors

bepursuant avatar foobarquaxx avatar tricki avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.