Package Data | |
---|---|
Maintainer Username: | jamesmills |
Maintainer Contact: | james@jamesmills.co.uk (James Mills) |
Package Create Date: | 2017-08-30 |
Package Last Update: | 2023-02-15 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2025-01-29 15:03:30 |
Package Statistics | |
---|---|
Total Downloads: | 4,428 |
Monthly Downloads: | 45 |
Daily Downloads: | 2 |
Total Stars: | 68 |
Total Watchers: | 4 |
Total Forks: | 14 |
Total Open Issues: | 1 |
Enable users to watch various models in your application.
watched
and unwatched
methodsPull in the package using Composer
composer require jamesmills/watchable
Note: If you are using Laravel 5.5, the next step for provider are unnecessary. Laravel Watchable supports Laravel Package Discovery.
Include the service provider within app/config/app.php
.
'providers' => [
...
JamesMills\Watchable\WatchableServiceProvider::class,
],
Publish and run the database migrations
php artisan vendor:publish --provider="JamesMills\Watchable\WatchableServiceProvider" --tag="migrations"
php artisan migrate
I wrote a blog post to give you some boilerplate code that you can use in your application to wrap around the Laravel Watchable package.
https://blog.jamesmills.co.uk/2017/10/22/laravel-watchable-package
Simply add the watchable
trait to your model
use Illuminate\Database\Eloquent\Model;
use JamesMills\Watchable\Traits\Watchable;
class Book extends Model {
use Watchable;
}
Watch a model
$book = Book::first();
$book->watch();
Unwatch a model
$book = Book::first();
$book->unwatch();
Toggle the watching of a model
$book = Book::first();
$book->toggleWatch();
You can optionally send the $user_id
if you don't want to use the built in auth()->user()->id
functionality.
$book = Book::first();
$book->watch($user_id);
$book->unwatch($user_id);
$book->toggleWatch($user_id);
Find out if the current user is watching the model
@if ($book->isWatched())
{{ You are watching this book }}
@else
{{ You are NOT watching this book }}
@endif
Get a collection of the user who are watching a model
$book = Book::first();
$book->collectWatchers();
One of the main reasons I built this package was to scratch my own itch with an application I am building. I wanted to be able to send notifications to user who were watching a given model and I also wanted to allow users to be able to watch a number of different models.
public function pause(Order $order)
{
$this->performAction('paused', $order);
Notification::send($order->collectWatchers(), new OrderPaused($order));
}
The MIT License (MIT). Please see License File for more information.