Laravel Likeable is a package designed to add like and dislike functionality to your Laravel applications. It allows users to easily express their opinions on content (such as posts, comments, or any other models) by liking or disliking them, enhancing user engagement on your platform.
Key Features
- Easy Integration: Simple setup to add like/dislike functionality to your Eloquent models.
- Flexible: Can be used with various models, such as posts, comments, or any custom model.
- User-Friendly: Allows users to like or dislike content with ease, improving interaction.
- Statistics: Get statistics on likes and dislikes, allowing you to analyze user engagement.
Installation
To install Laravel Likeable, you can use Composer. Run the following command in your terminal:
1 |
composer require "kordy/laravel-likeable" |
Configuration
After installing the package, you need to publish the configuration file and migrations:
1 |
php artisan vendor:publish --provider="Kordy\Likeable\LikeableServiceProvider" |
This will create the necessary configuration file and migration files.
Migration
Run the migrations to create the likes
table in your database:
1 |
php artisan migrate |
Setting Up Your Model
To enable likeable functionality in your model, you need to use the Likeable
trait. For example, if you want to add like functionality to a Post
model, your model should look like this:
1 2 3 4 5 6 7 8 9 10 11 |
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Kordy\Likeable\Likeable; class Post extends Model { use Likeable; // Your model properties and methods here } |
Liking and Disliking
With Laravel Likeable, you can easily implement the like and dislike functionality in your controllers or routes.
Liking a Post
You can like a post using the like
method:
1 2 |
$post = Post::find($postId); $post->like($userId); // Like the post |
Disliking a Post
Similarly, you can dislike a post:
1 |
$post->dislike($userId); // Dislike the post |
Removing a Like or Dislike
To remove a like or dislike from a post, you can use the unlike
or undislike
methods:
1 2 |
$post->unlike($userId); // Remove like $post->undislike($userId); // Remove dislike |
Checking Likes and Dislikes
You can check if a user has liked or disliked a post:
1 2 3 4 5 6 7 |
if ($post->isLikedBy($userId)) { // User has liked the post } if ($post->isDislikedBy($userId)) { // User has disliked the post } |
Retrieving Likes and Dislikes
You can retrieve the total number of likes and dislikes for a post:
1 2 |
$totalLikes = $post->likes()->count(); $totalDislikes = $post->dislikes()->count(); |
Example Usage
Here’s a simple example of how you might implement liking functionality in a controller method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
namespace App\Http\Controllers; use App\Models\Post; use Illuminate\Http\Request; class PostController extends Controller { public function like(Request $request, $id) { $post = Post::findOrFail($id); $post->like($request->user()->id); return response()->json(['message' => 'Post liked successfully!', 'likes' => $post->likes()->count()]); } public function dislike(Request $request, $id) { $post = Post::findOrFail($id); $post->dislike($request->user()->id); return response()->json(['message' => 'Post disliked successfully!', 'dislikes' => $post->dislikes()->count()]); } } |
Conclusion
Laravel Likeable makes it easy to add like and dislike functionality to your Laravel applications. By following the steps outlined above, you can quickly integrate this feature and enhance user engagement on your platform.