Posts Learn Components Snippets Categories Tags Tools About
/

Laravel Likeable: Like and Dislike Eloquent Models

Learn how to implement simple like system to allow user to like or dislike eloquent models with Laravel Likeable package

2 years ago

7 mins read

3593 views

In this tutorial, you'll be learning how to implement a simple-like system in Laravel which essentially allows your Laravel Eloquent models to be liked or disliked aka "upvotes" or "downvoted" with the help of Laravel Likeable Package.

The steps are very straightforward just follow along and you will have it implemented in a matter of minutes. For this case scenario, imagine that you have Post or Article models and you want the user to like or dislike it, this package does exactly that.

Installation


Require the package to your app using composer.
composer require rtconner/laravel-likeable

Migrate Table


This package comes with a migration file that contains the table structure to save the liked model.
php artisan migrate

Setup Model


To allow the user to like the particular model, make use of the Likeable trait from the package itself.
class Post extends \Illuminate\Database\Eloquent\Model {
	use \Conner\Likeable\Likeable;
}

Implementation


To like the model (in this case post model) call the like method that's made available by the trait.
$post->like(); // like the article for current user
$post->like($userId); // pass in your own user id
$post->like(0); // just add likes to the count, and don't track by user
In contrast, to dislike the model, call the unlike method.
$post->unlike(); // remove like from the article
$post->unlike($userId); // pass in your own user id
$post->unlike(0); // remove likes from the count -- does not check for user
Depending on the params passed into the like or unlike, we can control who actually performs the action to the model. e.g the authenticated user, the user-specified or anonymous user.

Count total number of Likes


To count the total number of likes, do access the likeCount property
$post->likeCount; // get count of likes
In some cases you might want to loop through the likes collection, to do so just access the collection instance.
$post->likes; // Iterable Illuminate\Database\Eloquent\Collection of existing likes
Some other checking may also be necessary for an existing user, it's done by doing so.
$post->liked(); // check if currently logged in user liked the article
$post->liked($userId);

Performance Enhancement


To better enhance the performance, do eager load the likes relation by using with key.
Post::whereLikedBy($userId) // find only articles where user liked them
	->with('likeCounter') // highly suggested to allow eager load
	->get();
By now you should be able to make any of your model instances likable or dislikeable. Thanks for reading and have a good try. If there's any issue just comment it down below.

Alternative Tags

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

)