modularavel/likeable
| Install | |
|---|---|
composer require modularavel/likeable |
|
| Latest Version: | 1.0.0 |
| PHP: | >=8.0 |
| License: | MIT |
| Last Updated: | Oct 1, 2025 |
| Links: | GitHub · Packagist |
Modularavel Likeable
A Laravel Livewire package for adding like and dislike functionality to your Eloquent models with beautiful TailwindCSS styled components.
Features
- Like and dislike any Eloquent model
- Real-time updates with Livewire
- Support for authenticated users and guest tracking via IP
- Beautiful TailwindCSS styled button component
- Dark mode support
- Configurable options
- Easy to integrate and use
Requirements
- PHP 8.0 or higher
- Laravel 9.x, 10.x, or 11.x
- Livewire 2.x or 3.x
- TailwindCSS
Installation
1. Install the package via Composer
composer require modularavel/likeable
2. Publish the migrations
php artisan vendor:publish --tag=likeable-migrations
3. Run migrations
php artisan migrate
4. (Optional) Publish the configuration file
php artisan vendor:publish --provider="Modularavel\Likeable\LikeableServiceProvider" --tag=config
5. (Optional) Publish the views for customization
php artisan vendor:publish --tag=likeable-views
Usage
1. Add the Likeable trait to your model
use Modularavel\Likeable\Traits\Likeable;
class Post extends Model
{
use Likeable;
// Your model code...
}
2. Use the Livewire component in your Blade views
<livewire:likeable::like-button :model="$post" />
Or with custom options:
<livewire:likeable::like-button
:model="$post"
:show-count="true"
:show-dislike="true"
/>
You can also pass the model ID and type separately:
<livewire:likeable::like-button
:model-id="$post->id"
:model-type="App\Models\Post::class"
/>
3. Available Methods
The Likeable trait provides these methods:
// Like/Dislike actions
$post->like();
$post->dislike();
// Get counts
$post->likesCount();
$post->dislikesCount();
// Check if liked/disliked by current user or IP
$post->isLikedBy();
$post->isDislikedBy();
// Relationships
$post->likes;
$post->dislikes;
$post->allLikes;
4. Using programmatically
// Like a post
$post->like(); // Uses current authenticated user or IP
// Dislike a post
$post->dislike();
// Like/Dislike with specific user
$post->like($userId);
$post->dislike($userId);
// Get like count
$likesCount = $post->likesCount();
$dislikesCount = $post->dislikesCount();
// Check if user has liked
if ($post->isLikedBy()) {
// User has liked this post
}
// Check if user has disliked
if ($post->isDislikedBy()) {
// User has disliked this post
}
Configuration
The configuration file allows you to customize the package behavior:
return [
// User model to use
'user_model' => App\Models\User::class,
// Track guests by IP address
'track_by_ip' => true,
// Show counts on buttons
'show_counts' => true,
// Enable/disable dislikes
'enable_dislikes' => true,
];
Customization
Styling
The component uses TailwindCSS classes. If you want to customize the appearance, publish the views:
php artisan vendor:publish --tag=likeable-views
Then edit the view files in resources/views/vendor/likeable/.
Dark Mode
The component includes built-in dark mode support using TailwindCSS dark mode classes.
Component Props
| Prop | Type | Default | Description |
|---|---|---|---|
model |
Model | null | The Eloquent model instance |
modelId |
int | null | The model ID (alternative to passing model) |
modelType |
string | null | The model class name (alternative to passing model) |
showCount |
bool | true | Show like/dislike counts |
showDislike |
bool | true | Show dislike button |
Events
The component emits these Livewire events:
likeToggled- Fired when like is toggleddislikeToggled- Fired when dislike is toggledrefreshLikes- Listen to this to refresh like counts
Example of listening to events:
<script>
Livewire.on('likeToggled', (modelId, modelType) => {
console.log('Like toggled for', modelType, modelId);
});
</script>
Database Schema
The package creates a likes table with the following structure:
id- Primary keylikeable_type- Polymorphic typelikeable_id- Polymorphic IDuser_id- User ID (nullable)ip_address- IP address (nullable)type- 'like' or 'dislike'created_at- Timestampupdated_at- Timestamp
License
MIT License
Support
For issues, questions, or contributions, please visit the GitHub repository.
Testing
composer test
Related Packages
Make Laravel Eloquent models reactable with any type of emotions in a minutes!
Implementation of a 'like' or 'favorite' or 'remember' feature. Supports for ses...