manzadey/laravel-like

Allows Laravel Eloquent models to implement a 'like'

v1.1.0 2022-05-24 03:01 UTC

This package is auto-updated.

Last update: 2024-04-24 07:11:19 UTC


README

Latest Version on Packagist Total Downloads Software License

This package implements the likes/dislikes system in the laravel framework.

Index

Installation

You can install the package via composer:

  1. Install the package via Composer
composer require manzadey/laravel-like
  1. Publish the database from the command line:
php artisan vendor:publish --provider="Manzadey\LaravelLike\LikeServiceProvider"
  1. Migrate the database from the command line:
php artisan migrate

Publishing the config file

Publishing the config file is optional:

php artisan vendor:publish --provider="Manzadey\LaravelLike\LikeServiceProvider" --tag="config"

Models

Your User model should import the Likeability trait, LikeabilityContract implements interface and use it, that trait allows the user to like the models. (see an example below):

use Manzadey\LaravelLike\Traits\Likeability;
use Manzadey\LaravelLike\Contracts\LikeabilityContract;

class User extends Authenticatable implements LikeabilityContract;
{
	use Likeability;
}

Your models should import the Likeable trait, LikeableContract implements interface and use it, that trait has the methods that you'll use to allow the model be likeable. In all the examples I will use the Post model as the model that is 'Likeable', that's for example proposes only. (see an example below):

use Manzadey\LaravelLike\Traits\Likeable;
use Manzadey\LaravelLike\Contracts\LikeableContract;

class Post extends Model implements LikeableContract
{
    use Likeable;
}

That's it ... your model is now "likeable"! Now the User can favorite models that have the favoriteable trait.

Usage

A standard set of methods for working with the package:

$article = \App\Models\Article::first();
$user = \App\Models\User::first();

$article->like($user); // Add the current model to like of the specified user
$article->dislike($user); // Add the current model to dislike of the specified user
$article->toggleLike($user); // The switch of the likes of the specified user
$article->removeLikeable($user); // Delete the current module from the likes of the specified user
$article->isLike($user); // Check the current model whether it is a likes of the specified user
$article->isDislike($user); // Check the current model whether it is a likes of the specified user

You can also use these methods as a `Likeability' model.

Checking the model for the presence of likes/dislikes of a particular user

$article = \App\Models\Article::first();
$user = \App\Models\User::first();

$article->isLike($user);
$article->isDislike($user);

You can also use these methods as a `Likeability' model.

Check the current model, whether it is marked with a like/dislike

$article = \App\Models\Article::first();

$article->isLikes();
$article->isDislikes();

Returns a collection of users who have liked/disliked this model

$article = \App\Models\Article::first();
$user = \App\Models\User::first();

$article->likeBy();
$article->dislikeBy();

We get a collection of models marked with a like/dislike

$article = \App\Models\Article::first();
$user = \App\Models\User::first();

$user->getLike();
$user->getDisike();

// With usage models
$user->getLike([\App\Models\Article::class]);
$user->getDisike([\App\Models\Article::class]);

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email andrey.manzadey@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.