Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Likebundle Laravel Package

xlabs/likebundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require xlabs/likebundle
    

    Publish the bundle's configuration and migrations:

    php artisan xlabs:like:install
    
  2. Database Setup Run migrations to create the likes table:

    php artisan migrate
    
  3. Basic Usage Add the Likeable trait to your model (e.g., Post.php):

    use XLabs\LikeBundle\Traits\Likeable;
    
    class Post extends Model
    {
        use Likeable;
    }
    
  4. First Like Action Toggle a like on a post in a controller:

    $post = Post::find(1);
    $post->toggleLike(auth()->id());
    
  5. Displaying Likes Use the provided helpers in Blade:

    @like($post, 'like-button', 'liked')
    

Implementation Patterns

Core Workflows

  1. Model Integration

    • Extend any Eloquent model with Likeable trait.
    • Automatically adds likes() relationship and toggleLike() method.
  2. API Endpoints Create a dedicated API route for likes (e.g., like/{model}/{id}):

    Route::post('like/{model}/{id}', [LikeController::class, 'toggle']);
    
  3. Real-Time Updates Use Laravel Echo + Pusher to broadcast like events:

    // In LikeController
    broadcast(new LikeEvent($model, $userId, $liked));
    
  4. Batch Processing Use XLabs\LikeBundle\Services\LikeService to manage bulk operations:

    $service = app(LikeService::class);
    $service->bulkToggle($postIds, auth()->id());
    

Integration Tips

  • Validation: Extend the LikeRequest class to add custom validation rules.
  • Middleware: Protect like actions with auth middleware.
  • Caching: Cache like counts for high-traffic models:
    Cache::remember("post:{$post->id}:likes", now()->addHours(1), fn() => $post->likesCount());
    

Gotchas and Tips

Common Pitfalls

  1. Model Configuration

    • Ensure your model uses XLabs\LikeBundle\Traits\Likeable before defining relationships.
    • If using polymorphic likes, set public $likeableType = 'App\Models\YourModel';.
  2. Migration Conflicts

    • If the likes table already exists, manually adjust the migration or drop it first:
      php artisan migrate:fresh --env=testing
      
  3. Event Listeners

    • The bundle fires Liked and Unliked events. Subscribe to them in EventServiceProvider:
      protected $listen = [
          'XLabs\LikeBundle\Events\Liked' => [
              YourListener::class,
          ],
      ];
      
  4. Performance

    • Avoid eager-loading likes for models with many likes. Use withCount('likes') instead:
      $posts = Post::withCount('likes')->get();
      

Debugging Tips

  • Log Like Actions: Extend the LikeService to log actions:

    public function toggleLike($model, $userId, $liked)
    {
        \Log::info("Like toggled", [
            'model' => $model->id,
            'user' => $userId,
            'action' => $liked ? 'liked' : 'unliked',
        ]);
        // ... rest of the logic
    }
    
  • Check for Duplicate Entries: Use unique() in migrations or add a composite unique index:

    Schema::table('likes', function (Blueprint $table) {
        $table->unique(['likeable_type', 'likeable_id', 'user_id']);
    });
    

Extension Points

  1. Custom Like Logic Override the toggleLike() method in your model:

    public function toggleLike($userId, $force = null)
    {
        if ($this->isProUser($userId)) {
            return $this->forceLike($userId);
        }
        return parent::toggleLike($userId, $force);
    }
    
  2. Custom Storage Replace the default Like model by binding a custom implementation in AppServiceProvider:

    public function register()
    {
        $this->app->bind(
            XLabs\LikeBundle\Models\Like::class,
            CustomLikeModel::class
        );
    }
    
  3. UI Customization Override the Blade directives in AppServiceProvider:

    Blade::directive('like', function ($expression) {
        return "<?php echo XLabs\LikeBundle\Blade::like($expression); ?>";
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
snowsoft/faker
meditationsteps/blogetc
animalyoung/laravel-influxdb
onix-solutions/sms-telera
lonely-lights/prosetta
oxygen/data
oxygen/core
jengo/base
xiloryx/log-collector
a21ns1g4ts/filament-stripe
la-php/laravel-admin
artisan-build/community-prompts
vleroy/smart
ayles-software/laravel-australian-public-holidays
cboxdk/laravel-id-spatie
cboxdk/laravel-id-billing
cboxdk/laravel-id-billing-bridge
simple-as-fuck/php-performance-log
hsluv/hsluv
seampact/codegen