IMPORTANT: This is not a package for managing subscribers related to memberships and payed methods. It is used for managing user/subscriber relations just like in YouTube for example, where the model can be subscribed by user.
This package is based on an idea and an architecture of the Laravel Love package, although the functionality is different.
- Uses UUIDs instead of integers (your user model must use them as well!)
- Designed to work with Laravel Eloquent models.
- Using contracts to keep high customization capabilities.
- Using traits to get functionality out of the box.
- Most part of the the logic is handled by the
SubscribeableService
. - Has Artisan command
gosubscribe:recount {model?}
to re-fetch subscribe counters. - Subscribes for one model are mutually exclusive.
- Get Subscribeable models ordered by subscribe count.
- Events for
subscribe
,unsubscribe
methods. - Following PHP Standard Recommendations:
- Covered with unit tests.
First, pull in the package through Composer.
$ composer require gorankrgovic/laravel-subscribe
At last you need to publish and run database migrations.
$ php artisan migrate
If you want to make changes in migrations, publish them to your application first.
$ php artisan vendor:publish --provider="Gox\Laravel\Subscribe\Providers\SubscribeServiceProvider" --tag=migrations
Use Gox\Contracts\Subscribe\Subscriber\Models\Subscriber
contract in model which will get subscribes
behavior and implement it or just use Gox\Laravel\Subscribe\Subscriber\Models\Traits\Subscriber
trait.
use Gox\Contracts\Subscribe\Subscriber\Models\Subscriber as SubscriberContract;
use Gox\Laravel\Subscribe\Subscriber\Models\Traits\Subscriber;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements SubscriberContract
{
use Subscriber;
}
Use Gox\Contracts\Subscribe\Subscribeable\Models\Subscribeable
contract in model which will get subscribes
behavior and implement it or just use Gox\Laravel\Subscribe\Subscribeable\Models\Traits\Subscribeable
trait.
use Gox\Contracts\Subscribe\Subscribeable\Models\Subscribeable as SubscribeableContract;
use Gox\Laravel\Subscribe\Subscribeable\Models\Traits\Subscribeable;
use Illuminate\Database\Eloquent\Model;
class Article extends Model implements SubscribeableContract
{
use Subscribeable;
}
$user->subscribe($article);
$article->subscrubeBy(); // current user
$article->subscribeBy($user->id);
$user->unsubscribe($article);
$article->unsubscribeBy(); // current user
$article->unsubscribeBy($user->id);
$article->subcribesCount;
$article->subscribesCounter;
$article->subscribes();
$article->subscribes;
$user->hasSubscribed($article);
$article->subscribed; // current user
$article->isSubscribedBy(); // current user
$article->isSubscribedBy($user->id);
Checks in eager loaded relations subscribes
first.
$article->collectSubscribers();
$article->removeSubscribes();
Article::whereSubscribedBy($user->id)
->with('subscribesCounter') // Allow eager load (optional)
->get();
$sortedArticles = Article::orderBySubscribesCount()->get();
$sortedArticles = Article::orderBySubscribesCount('asc')->get();
Uses desc
as default order direction.
On each subscribe added \Gox\Laravel\Subscribe\Subscribeable\Events\SubscribeableWasSubscribed
event is fired.
On each subscribe removed \Gox\Laravel\Subscribe\Subscribeable\Events\SubscribeableWasUnsubscribed
event is fired.
$ gosubscribe:recount
$ gosubscribe:recount --model="article"
$ gosubscribe:recount --model="App\Models\Article"
You can override core classes of package with your own implementations:
Gox\Laravel\Subscribe\Subscribe\Models\Subscribe
Gox\Laravel\Subscribe\SubscribeCounter\Models\SubscribeCounter
Gox\Laravel\Subscribe\Subscribeable\Services\SubscribeableService
Note: Don't forget that all custom models must implement original models interfaces.
To make it you should use container binding interfaces to implementations in your application service providers.
$this->app->bind(
\Gox\Contracts\Subscribe\Subscribe\Models\Subscribe::class,
\App\Models\CustomSubscribe::class
);
After that your CustomSubscribe
class will be instantiable with helper method app()
.
$model = app(\Gox\Contracts\Subscribe\Subscribe\Models\Subscribe::class);
Run the tests with:
$ vendor/bin/phpunit
If you discover any security related issues, please email me instead of using the issue tracker.
Laravel Subscribe
package is open-sourced software licensed under the MIT license by Goran Krgovic.