namest/likeable

v0.1.2 2015-03-01 20:50 UTC

This package is not auto-updated.

Last update: 2024-12-21 17:59:01 UTC


README

Provide an elegant way to interact with like feature between your eloquent models.

Note: Package only support Laravel 5

Installation

Step 1: Install package

composer require namest/likeable

Step 2: Register service provider in your config/app.php

return [
    ...
    'providers' => [
        ...
        'Namest\Likeable\LikeableServiceProvider',
    ],
    ...
];

Step 3: Publish package resources, include: configs, migrations. Open your terminal and type:

php artisan vendor:publish --provider="Namest\Likeable\LikeableServiceProvider"

Step 4: Migrate the migration that have been published

php artisan migrate

Step 5: Use some traits to make awesome things

class User extends Model
{
    use \Namest\Likeable\LikerTrait;
    
    // ...
}

class Post extends Model
{
    use \Namest\Likeable\LikeableTrait;
    
    // ...
}

Step 6: Read API below and start happy

API

$user = \App\User::find(1);
$post = \App\Post::find(2);

$like = $user->like($post); // Return Namest\Likeable\Like instance
$result = $user->unlike($post); // Return true when success and false on otherwise
$user = $like->liker; // Return model that like another model
$post = $like->likeable; // Return model that was liked by another model
$posts = $user->likes; // Return likeable collection that liker was liked
$users = $post->likers; // Return liker collection who like that post
$users = User::wasLike($post)->...->get(); // Return liker collection who like that post
$posts = Post::likedBy($user)->...->get(); // Return post collection which was liked by the user 

Events

namest.likeable.liking

When: Before $liker like a likeable

Payloads:

  • $liker: Who do this action
  • $likeable: Which will be liked

Usage:

\Event::listen('namest.likeable.liking', function ($liker, $likeable) {
    // Do something
});

namest.likeable.liked

When: After $liker was like a likeable

Payloads:

  • $liker: Who do this action
  • $likeable: Which was liked
  • $like: Like instance

Usage:

\Event::listen('namest.likeable.liked', function ($liker, $likeable, $like) {
    // Do something
});

namest.likeable.unliking

When: Before $liker unlike a likeable

Payloads:

  • $liker: Who do this action
  • $likeable: Which will be unliked

Usage:

\Event::listen('namest.likeable.unliking', function ($liker, $likeable) {
    // Do something
});

namest.likeable.unliked

When: After $liker was unlike a likeable

Payloads:

  • $liker: Who do this action
  • $likeable: Which was unliked

Usage:

\Event::listen('namest.likeable.unliked', function ($liker, $likeable) {
    // Do something
});