Skip to content

It lets people express how they feel about the content. React on Eloquent models with Likes or Dislikes.

License

Notifications You must be signed in to change notification settings

MattMangoni/laravel-love

 
 

Repository files navigation

Laravel Love

cog-laravel-love

Releases Build Status StyleCI Code Quality License

Introduction

Laravel Love simplify management of Eloquent model's likes & dislikes reactions. Make any model reactable with likeable & dislikeable in a minutes!

This package is a fork of the abandoned Laravel Likeable. It completely changes package namespace architecture, aimed to API refactoring and adding new features.

Contents

Features

  • 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 LikeableService.
  • Has Artisan command love:recount {model?} {type?} to re-fetch likes counters.
  • Likeable model can has Likes and Dislikes.
  • Likes and Dislikes for one model are mutually exclusive.
  • Get Likeable models ordered by likes count.
  • Events for like, unlike, dislike, undislike methods.
  • Following PHP Standard Recommendations:
  • Covered with unit tests.

Installation

First, pull in the package through Composer.

$ composer require cybercog/laravel-love

Perform Database Migration

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="Cog\Laravel\Love\Providers\LoveServiceProvider" --tag=migrations

Usage

Prepare Liker Model

Use Cog\Contracts\Love\Liker\Models\Liker contract in model which will get likes behavior and implement it or just use Cog\Laravel\Love\Liker\Models\Traits\Liker trait.

use Cog\Contracts\Love\Liker\Models\Liker as LikerContract;
use Cog\Laravel\Love\Liker\Models\Traits\Liker;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements LikerContract
{
    use Liker;
}

Prepare Likeable Model

Use Cog\Contracts\Love\Likeable\Models\Likeable contract in model which will get likes behavior and implement it or just use Cog\Laravel\Love\Likeable\Models\Traits\Likeable trait.

use Cog\Contracts\Love\Likeable\Models\Likeable as LikeableContract;
use Cog\Laravel\Love\Likeable\Models\Traits\Likeable;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements LikeableContract
{
    use Likeable;
}

Available Methods

Likes

Like model
$user->like($article);

$article->likeBy(); // current user
$article->likeBy($user->id);
Remove like mark from model
$user->unlike($article);

$article->unlikeBy(); // current user
$article->unlikeBy($user->id);
Toggle like mark of model
$user->toggleLike($article);

$article->toggleLikeBy(); // current user
$article->toggleLikeBy($user->id);
Get model likes count
$article->likesCount;
Get model likes counter
$article->likesCounter;
Get likes relation
$article->likes();
Get iterable Illuminate\Database\Eloquent\Collection of existing model likes
$article->likes;
Boolean check if user liked model
$user->hasLiked($article);

$article->liked; // current user
$article->isLikedBy(); // current user
$article->isLikedBy($user->id);

Checks in eager loaded relations likes & likesAndDislikes first.

Get collection of users who liked model
$article->collectLikers();
Delete all likes for model
$article->removeLikes();

Dislikes

Dislike model
$user->dislike($article);

$article->dislikeBy(); // current user
$article->dislikeBy($user->id);
Remove dislike mark from model
$user->undislike($article);

$article->undislikeBy(); // current user
$article->undislikeBy($user->id);
Toggle dislike mark of model
$user->toggleDislike($article);

$article->toggleDislikeBy(); // current user
$article->toggleDislikeBy($user->id);
Get model dislikes count
$article->dislikesCount;
Get model dislikes counter
$article->dislikesCounter;
Get dislikes relation
$article->dislikes();
Get iterable Illuminate\Database\Eloquent\Collection of existing model dislikes
$article->dislikes;
Boolean check if user disliked model
$user->hasDisliked($article);

$article->disliked; // current user
$article->isDislikedBy(); // current user
$article->isDislikedBy($user->id);

Checks in eager loaded relations dislikes & likesAndDislikes first.

Get collection of users who disliked model
$article->collectDislikers();
Delete all dislikes for model
$article->removeDislikes();

Likes and Dislikes

Get difference between likes and dislikes
$article->likesDiffDislikesCount;
Get likes and dislikes relation
$article->likesAndDislikes();
Get iterable Illuminate\Database\Eloquent\Collection of existing model likes and dislikes
$article->likesAndDislikes;

Scopes

Find all articles liked by user
Article::whereLikedBy($user->id)
    ->with('likesCounter') // Allow eager load (optional)
    ->get();
Find all articles disliked by user
Article::whereDislikedBy($user->id)
    ->with('dislikesCounter') // Allow eager load (optional)
    ->get();
Fetch Likeable models by likes count
$sortedArticles = Article::orderByLikesCount()->get();
$sortedArticles = Article::orderByLikesCount('asc')->get();

Uses desc as default order direction.

Fetch Likeable models by dislikes count
$sortedArticles = Article::orderByDislikesCount()->get();
$sortedArticles = Article::orderByDislikesCount('asc')->get();

Uses desc as default order direction.

Events

On each like added \Cog\Laravel\Love\Likeable\Events\LikeableWasLiked event is fired.

On each like removed \Cog\Laravel\Love\Likeable\Events\LikeableWasUnliked event is fired.

On each dislike added \Cog\Laravel\Love\Likeable\Events\LikeableWasDisliked event is fired.

On each dislike removed \Cog\Laravel\Love\Likeable\Events\LikeableWasUndisliked event is fired.

Console Commands

Recount likes and dislikes of all model types
$ love:recount
Recount likes and dislikes of concrete model type (using morph map alias)
$ love:recount --model="article"
Recount likes and dislikes of concrete model type (using fully qualified class name)
$ love:recount --model="App\Models\Article"
Recount only likes of all model types
$ love:recount --type="LIKE"
Recount only likes of concrete model type (using morph map alias)
$ love:recount --model="article" --type="LIKE"
Recount only likes of concrete model type (using fully qualified class name)
$ love:recount --model="App\Models\Article" --type="LIKE"
Recount only dislikes of all model types
$ love:recount --type="DISLIKE"
Recount only dislikes of concrete model type (using morph map alias)
$ love:recount --model="article" --type="DISLIKE"
Recount only dislikes of concrete model type (using fully qualified class name)
$ love:recount --model="App\Models\Article" --type="DISLIKE"

Extending

You can override core classes of package with your own implementations:

  • Cog\Laravel\Love\Like\Models\Like
  • Cog\Laravel\Love\LikeCounter\Models\LikeCounter
  • Cog\Laravel\Love\Likeable\Services\LikeableService

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.

Use model class own implementation
$this->app->bind(
    \Cog\Contracts\Love\Like\Models\Like::class,
    \App\Models\CustomLike::class
);
Use service class own implementation
$this->app->singleton(
    \Cog\Contracts\Love\Likeable\Services\LikeableService::class,
    \App\Services\CustomService::class
);

After that your CustomLike and CustomService classes will be instantiable with helper method app().

$model = app(\Cog\Contracts\Love\Like\Models\Like::class);
$service = app(\Cog\Contracts\Love\Likeable\Services\LikeableService::class);

Changelog

Please see CHANGELOG for more information on what has changed recently.

Upgrading

Please see UPGRADING for detailed upgrade instructions.

Contributing

Please see CONTRIBUTING for details.

Testing

Run the tests with:

$ vendor/bin/phpunit

Security

If you discover any security related issues, please email open@cybercog.su instead of using the issue tracker.

Contributors

@antonkomarev
Anton Komarev
@squigg
Squigg
@acidjazz
Kevin Olson
@raniesantos
Ranie Santos

Laravel Love contributors list

Alternatives

Feel free to add more alternatives as Pull Request.

License

  • Laravel Love package is open-sourced software licensed under the MIT license by Anton Komarev.
  • Devil image licensed under Creative Commons 3.0 by YuguDesign.

About CyberCog

CyberCog is a Social Unity of enthusiasts. Research best solutions in product & software development is our passion.

CyberCog

About

It lets people express how they feel about the content. React on Eloquent models with Likes or Dislikes.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%