Git Product home page Git Product logo

watchable's Introduction

Laravel Watchable

Packagist Packagist Travis Packagist Buy us a tree Treeware (Trees)

Enable users to watch various models in your application.

  • Designed to work with Laravel Eloquent models
  • Just add the trait to the model you would like to be watchable
  • Watches are unique for one model and one user
  • Events are fired on watched and unwatched methods
  • Built to work with Laravel Notifications

Installation

Pull 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

Sample Usage and Boilerplate

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://jamesmills.co.uk/2017/10/22/laravel-watchable-package

How to use

Prepare your model to be watched

Simply add the watchable trait to your model

use Illuminate\Database\Eloquent\Model;
use JamesMills\Watchable\Traits\Watchable;

class Book extends Model {
    use Watchable;
} 

Available methods

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(); 

Use with Notifications

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));
}

License

This package is 100% free and open-source, under the MIT license. Use it however you want.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

watchable's People

Contributors

jamesmills avatar lrljoe avatar madmikeyb avatar anthonysterling avatar cmgmyr avatar furqanfreed avatar carusogabriel avatar rrrhys avatar

Stargazers

 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.