| Install | |
|---|---|
composer require modularavel/likeable |
|
| Latest Version: | 1.0.0 |
| PHP: | >=8.0 |
A Laravel Livewire package for adding like and dislike functionality to your Eloquent models with beautiful TailwindCSS styled components.
composer require modularavel/likeable
php artisan vendor:publish --tag=likeable-migrations
php artisan migrate
php artisan vendor:publish --provider="Modularavel\Likeable\LikeableServiceProvider" --tag=config
php artisan vendor:publish --tag=likeable-views
use Modularavel\Likeable\Traits\Likeable;
class Post extends Model
{
use Likeable;
// Your model code...
}
<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"
/>
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;
// 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
}
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,
];
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/.
The component includes built-in dark mode support using TailwindCSS dark mode classes.
| 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 |
The component emits these Livewire events:
likeToggled - Fired when like is toggleddislikeToggled - Fired when dislike is toggledrefreshLikes - Listen to this to refresh like countsExample of listening to events:
<script>
Livewire.on('likeToggled', (modelId, modelType) => {
console.log('Like toggled for', modelType, modelId);
});
</script>
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 - TimestampMIT License
For issues, questions, or contributions, please visit the GitHub repository.
composer test