Posts Learn Components Snippets Categories Tags 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

4 months ago

7 mins read

1658 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.
new

PostSrc Code Snippets

Learn new snippets today, level up your Laravel, Alpine JS, Vue JS, Tailwind CSS skills and more.

Learn New Snippets

Authored by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your project.

View Components