定制 modularavel/likeable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

modularavel/likeable

最新稳定版本:1.0.0

Composer 安装命令:

composer require modularavel/likeable

包简介

A Laravel Livewire package for adding like/dislike functionality to Eloquent models

README 文档

README

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 toggled
  • dislikeToggled - Fired when dislike is toggled
  • refreshLikes - 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 key
  • likeable_type - Polymorphic type
  • likeable_id - Polymorphic ID
  • user_id - User ID (nullable)
  • ip_address - IP address (nullable)
  • type - 'like' or 'dislike'
  • created_at - Timestamp
  • updated_at - Timestamp

License

MIT License

Support

For issues, questions, or contributions, please visit the GitHub repository.

Testing

composer test

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-01