PHP code example of michaeljennings / feed

1. Go to this page and download the library: Download michaeljennings/feed library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

michaeljennings / feed example snippets


$user = User::find(1);
$team = Team::find(1);

// Push notification to a user
$feed->push('This is a new notification', $user);

// Push notification to a user, and a team of users
$feed->push('This is a new notification', [$user, $team]);

// Push notification to a user with multiple parameters
$feed->push([
    'icon' => 'icon-alert',
    'title' => 'Something Broke!',
    'body' => 'Something super important broke'
], $user);

// Get all of the notifications for a user
$notifications = $feed->pull($user);

// Get 10 notifications for the user
$notifications = $feed->limit(10)->pull($user);

// Mark a notification as read
$feed->markAsRead($notification);

'providers' => [

  Michaeljennings\Feed\FeedServiceProvider::class

];

'drivers' => [
    'eloquent' => [
        'model' => 'Path\To\Notification', // Update this to your notification model.
    ],
]

// Example of extending the model
class Notification extends \Michaeljennings\Feed\Store\Eloquent\Notification
{
    public function foo()
    {
        //
    }
}

// Example of just implementing the interfaces
class Notification implements \Michaeljennings\Feed\Contracts\Notification, \Michaeljennings\Feed\Contracts\Store
{
    public function bar()
    {
        //
    }
}

// Here we grab the driver manager and then extend it to a new 'foo' driver.
// Ideally this would be done within a service provider. 
app('feed.manager')->extend('foo', function($app) {
    return new Foo();
});

// The Foo driver needs to implement the Store interface so it has all of 
// the necessary methods.
class Foo implements Michaeljennings\Feed\Contracts\Store
{
    //
}

// The in the config file set the driver to our new foo driver.
return [
    'driver' => 'foo'
]

public function __construct(
    Michaeljennings\Feed\Contracts\PullFeed $pullFeed,
    Michaeljennings\Feed\Contracts\PushFeed $pushFeed
) {
    $this->pullFeed = $pullFeed;
    $this->pushFeed = $pushFeed;
}

$feed = feed();

'aliases' => [
    'Feed' => Michaeljennings\Feed\Facades\Feed::class
]

use Michaeljennings\Feed\Contracts\Notifiable as NotifiableContract;
use Michaeljennings\Feed\Notifications\Notifiable;

class User extends Model implements NotifiableContract
{
    use Notifiable;
}

use Michaeljennings\Feed\Contracts\NotifiableGroup;

class Team extends Model implements NotifiableGroup
{
    public function users()
    {
        return $this->hasMany('App\User');
    }

    public function getGroup()
    {
        return $this->users;
    }
}

$feed->push('My awesome notification', $user);
$feed->push('My awesome notification', [$user, $team]);
$feed->push([
    'title' => 'New Notification',
    'body' => 'My awesome notification'
], $user);

protected $listen = [
    'Michaeljennings\Feed\Events\NotificationAdded' => [
        'App\Listeners\BroadcastNotification',
        'App\Listeners\EmailNotification',
    ],
];

$feed->pull($user);

$feed->pullRead($user);

$feed->limit(10)->pull($user);
$feed->limit(10)->pullRead($user);

$feed->offset(10)->pull($user);
$feed->offset(10)->pullRead($user);

$feed->paginate(10)->pull($user);
$feed->paginate(10)->pullRead($user);

$feed->filter(function($query) {
    $query->where('icon', 'icon-alert');
})->pull($user);

$feed->filter(function($query) {
    $query->where('icon', 'icon-alert');
})->pullRead($user);

$feed->latest()->pull($user);
$feed->latest()->pullRead($user);

$feed->oldest()->pull($user);
$feed->oldest()->pullRead($user);

// Limit and offset the results
$feed->limit(10)->offset(10)->pull($user);


// Get all of the oldest read notifications, that have an alert icon.
$feed->filter(function($query) {
    $query->where('icon', 'icon-alert');
})->oldest()->pullRead($user);

$feed->markAsRead($notification);
$feed->read($notification);

protected $listen = [
    'Michaeljennings\Feed\Events\NotificationRead' => [
        'App\Listeners\BroadcastReadNotification',
    ],
];

$feed->markAsUnread($notification);
$feed->unread($notification);

protected $listen = [
    'Michaeljennings\Feed\Events\NotificationUnread' => [
        'App\Listeners\BroadcastUnreadNotification',
    ],
];