gorankrgovic/laravel-likeable

Make Laravel Eloquent models Likeable using UUIDs.

v0.0.4 2019-03-17 09:12 UTC

This package is auto-updated.

Last update: 2024-04-17 21:00:12 UTC


README

Introduction

This package is basically a simplified fork of a Laravel Love package with only using LIKE and UNLIKE capability. "Laravel Love" have more capabilities such as both "LIKE" and "DISLIKE" functionality.

Also, worth noting that this package utilizes usage of UUID's instead of integer ID's. And the "Likeable" and "Liker" models needs to utilize UUIDs as well. If you are not using UUID's please use the cybercog/laravel-likeable, cybercog/laravel-love or any of the alternatives listed below.

For the UUID generation this package uses Ramsey UUID.

Features

  • 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 LikeableService.
  • Has Artisan command golike:recount {model?} to re-fetch like counters.
  • Subscribes for one model are mutually exclusive.
  • Get Likeable models ordered by likes count.
  • Events for like, unlike methods.
  • Following PHP Standard Recommendations:

Alternatives

Installation

First, pull in the package through Composer.

$ composer require gorankrgovic/laravel-likeable

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="Gox\Laravel\Likeable\Providers\LikeableServiceProvider" --tag=migrations

Usage

Prepare Liker Model

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

use Gox\Contracts\Likeable\Liker\Models\Liker as LikerContract;
use Gox\Laravel\Likeable\Liker\Models\Traits\Liker;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements LikerContract
{
    use Liker;
}

Prepare Likeable Model

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

use Gox\Contracts\Likeable\Likeable\Models\Likeable as LikeableContract;
use Gox\Laravel\Likeable\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);
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 likes model
$user->hasLiked($article);

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

Checks in eager loaded relations likes first.

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

Scopes

Find all articles liked by user
Article::whereLikedBy($user->id)
    ->with('likesCounter') // 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.

Events

On each like added \Gox\Laravel\Subscribe\Subscribeable\Events\LikeableWasLiked event is fired.

On each like removed \Gox\Laravel\Subscribe\Subscribeable\Events\LikeableWasUnliked event is fired.

Console Commands

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

Security

If you discover any security related issues, please email me instead of using the issue tracker.

License

  • Laravel Likeable package is open-sourced software licensed under the MIT license by Goran Krgovic.
  • Laravel Love package is open-sourced software licensed under the MIT license by Anton Komarev.