Skip to content

πŸ”” User subscribe/unsubscribe behaviour for Laravel.

License

Notifications You must be signed in to change notification settings

laravel-interaction/subscribe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Laravel Subscribe

User subscribe/unsubscribe behaviour for Laravel.

Latest Stable Version Total Downloads Latest Unstable Version License

Introduction

It used for users to subscribe to the model(user/topic/channel) in order to receive notifications.

Installation

Requirements

Instructions

Require Laravel Subscribe using Composer.

composer require laravel-interaction/subscribe

Publish configuration and migrations

php artisan vendor:publish --tag=subscribe-config
php artisan vendor:publish --tag=subscribe-migrations

Run database migrations.

php artisan migrate

Usage

Setup Subscriber

use Illuminate\Database\Eloquent\Model;
use LaravelInteraction\Subscribe\Concerns\Subscriber;

class User extends Model
{
    use Subscriber;
}

Setup Subscribable

use Illuminate\Database\Eloquent\Model;
use LaravelInteraction\Subscribe\Concerns\Subscribable;

class Channel extends Model
{
    use Subscribable;
}

Subscriber

use LaravelInteraction\Subscribe\Tests\Models\Channel;
/** @var \LaravelInteraction\Subscribe\Tests\Models\User $user */
/** @var \LaravelInteraction\Subscribe\Tests\Models\Channel $channel */
// Subscribe to Subscribable
$user->subscribe($channel);
$user->unsubscribe($channel);
$user->toggleSubscribe($channel);

// Compare Subscribable
$user->hasSubscribed($channel);
$user->hasNotSubscribed($channel);

// Get subscribed info
$user->subscriberSubscriptions()->count(); 

// with type
$user->subscriberSubscriptions()->withType(Channel::class)->count(); 

// get subscribed channels
Channel::query()->whereSubscribedBy($user)->get();

// get subscribed channels doesnt subscribed
Channel::query()->whereNotSubscribedBy($user)->get();

Subscribable

use LaravelInteraction\Subscribe\Tests\Models\User;
use LaravelInteraction\Subscribe\Tests\Models\Channel;
/** @var \LaravelInteraction\Subscribe\Tests\Models\User $user */
/** @var \LaravelInteraction\Subscribe\Tests\Models\Channel $channel */
// Compare Subscriber
$channel->isSubscribedBy($user); 
$channel->isNotSubscribedBy($user);
// Get subscribers info
$channel->subscribers->each(function (User $user){
    echo $user->getKey();
});

$channels = Channel::query()->withCount('subscribers')->get();
$channels->each(function (Channel $channel){
    echo $channel->subscribers()->count(); // 1100
    echo $channel->subscribers_count; // "1100"
    echo $channel->subscribersCount(); // 1100
    echo $channel->subscribersCountForHumans(); // "1.1K"
});

Events

Event Fired
LaravelInteraction\Subscribe\Events\Subscribed When an object get subscribed.
LaravelInteraction\Subscribe\Events\Unsubscribed When an object get unsubscribed.

License

Laravel Subscribe is an open-sourced software licensed under the MIT license.