Giter VIP home page Giter VIP logo

laravel-likeable's Introduction

Laravel Likeable

cog-laravel-likeable

Build Status StyleCI Releases License

Attention

This package is abandoned and no longer maintained. Development moved to Laravel Love package!

If you already have installed version of Laravel Likeable you can use Laravel Love Migration Guide.

Introduction

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

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 likeable: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-likeable

If you are using Laravel 5.5 you can skip register package part.

Register package on Laravel 5.4 and lower

Include the service provider within app/config/app.php.

'providers' => [
    Cog\Likeable\Providers\LikeableServiceProvider::class,
],

Perform Database Migration

At last you need to publish and run database migrations.

$ php artisan vendor:publish --provider="Cog\Likeable\Providers\LikeableServiceProvider" --tag=migrations
$ php artisan migrate

Usage

Prepare likeable model

Use Likeable contract in model which will get likes behavior and implement it or just use Likeable trait.

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

class Article extends Model implements LikeableContract
{
    use Likeable;
}

Available methods

Likes

Like model
$article->like(); // current user
$article->like($user->id);
Remove like mark from model
$article->unlike(); // current user
$article->unlike($user->id);
Toggle like mark of model
$article->likeToggle(); // current user
$article->likeToggle($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
$article->liked; // current user
$article->liked(); // current user
$article->liked($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
$article->dislike(); // current user
$article->dislike($user->id);
Remove dislike mark from model
$article->undislike(); // current user
$article->undislike($user->id);
Toggle dislike mark of model
$article->dislikeToggle(); // current user
$article->dislikeToggle($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
$article->disliked; // current user
$article->disliked(); // current user
$article->disliked($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\Likeable\Events\ModelWasLiked event is fired.

On each like removed \Cog\Likeable\Events\ModelWasUnliked event is fired.

On each dislike added \Cog\Likeable\Events\ModelWasDisliked event is fired.

On each dislike removed \Cog\Likeable\Events\ModelWasUndisliked event is fired.

Console commands

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

Extending

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

  • Models\Like
  • Models\LikeCounter
  • 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\Likeable\Contracts\Like::class,
    \App\Models\CustomLike::class
);
Use service class own implementation
$this->app->singleton(
    \Cog\Likeable\Contracts\LikeableService::class,
    \App\Services\CustomService::class
);

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

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

Change log

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

Contributing

Please see CONTRIBUTING for details.

Testing

You can run the tests with:

$ vendor/bin/phpunit

Security

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

Contributors

@antonkomarev
Anton Komarev
@acidjazz
Kevin Olson

Laravel Likeable contributors list

Alternatives

Feel free to add more alternatives as Pull Request.

License

About CyberCog

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

CyberCog

laravel-likeable's People

Contributors

acidjazz avatar antonkomarev avatar squigg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-likeable's Issues

Too few arguments to function Laravel 5.5

Hello! To see if they could help me
In laravel 5.5 when I try to make a like or dislike (now I'm doing it massively with a seeder) the console returns this error

[Symfony\Component\Debug\Exception\FatalThrowableError] Type error: Too few arguments to function Illuminate\Database\Eloquent\Model::setAttribute(), 1 passed in .....

Any idea why? Thank you!

Add Likes Statistics Service

What to do

  1. Count likes of the Entity within date.
  2. Count dislikes of the Entity within date.
  3. Count likes\dislikes of the Entity within date.
  4. Get all the Entities which were liked within date (with likes meta info).
  5. Get all the Entities which were disliked within date (with likes meta info).
  6. Get all the Entities which were likes\disliked within date (with likes meta info).

API Design Mockup

Where Scopes

  • Add method scopeWhereLikedByDate to service layer.
  • Add method scopeWhereLikedByDate to Likeable models.
  • Add method scopeWhereLikedBetweenDates to service layer.
  • Add method scopeWhereLikedBetweenDates to Likeable models.
  • Add method scopeWhereDislikedByDate to service layer.
  • Add method scopeWhereDislikedByDate to Likeable models.
  • Add method scopeWhereDislikedBetweenDates to service layer.
  • Add method scopeWhereDislikedBetweenDates to Likeable models.

Counters

  • Add method countLikesByDate to service layer.
  • Add method countLikesByDate to Likeable models.
  • Add method countLikesBetweenDates to sevice layer.
  • Add method countLikesBetweenDates to Likeable models.

Split LikeableService by adding CounterService (?)

  • Move method incrementLikesCount to CounterService from LikeableService
  • Move method decrementLikesCount to CounterService from LikeableService
  • Move method incrementDislikesCount to CounterService from LikeableService
  • Move method decrementDislikesCount to CounterService from LikeableService
  • Move method removeLikeCountersOfType to CounterService from LikeableService
  • Move method scopeOrderByLikesCount to CounterService from LikeableService
  • Move method fetchLikesCounters to CounterService from LikeableService

Deleting a likeable model causes a SQL error

I'm not 100% sure the problem is this package, but when I delete a row from a Likeable model using ->delete() I get this error:

SQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect DOUBLE value: '1103d0bbff0e942fdf0d542e818f8ae2f88ba05d' (SQL: delete from like where (likeable_id = 102 and likeable_type = App\Models\Scene and type_id = like))

In my case I have id's in my DB that are hashes instead of just integers/floats.

My Article not Likeable anymore

hi when i try to like article getting problem
Type error: Argument 1 passed to Cog\Likeable\Services\LikeableService::addLikeTo()

Unable to get liked from a collection

I'm trying to get wether or not the current logged in user has liked items w/in a collection

I tried to do something like

$images = Image::with('likesCounter', 'liked');

And I get Call to a member function addEagerConstraints() on boolean

If I loop through my results and hit ->liked on each row I get a 45 query result instead of 5, is this possible at all?

weird issue when deleting a mongodb model

Not sure what's the problem here. Using latest version of your package, I prepared my model which is a mongodb model with hybrid relations to mysql models

I didn't actually do anything else with your package in this project yet.. I'm not using it yet.

If I try to do something as simple as delete-> or forceDelete() the model with the likeable trait I get this weird error

Type error: Argument 1 passed to Cog\Likeable\Observers\ModelObserver::deleted() must be an instance of Cog\Likeable\Contracts\Likeable, instance of App\Models\MyModel given

Where MyModel is the model I'm trying to delete. Not doing anything special here:

$submissions = Submission::has('media', '=', 0)->get();
        d("Deleting " . $submissions->count() . "submissions with no media attached");

        foreach ($submissions as $submission) {
            $submission->forceDelete();
        }

Any ideas?

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.