nanosolutions / laravel-likeable by nano
forked from rtconner/laravel-likeable

Trait for Laravel Eloquent models to allow easy implementation of a 'like' or 'favorite' or 'remember' feature.
457
3
3
Package Data
Maintainer Username: nano
Maintainer Contact: petr@nanosolutions.io (Petr Cervenka)
Package Create Date: 2016-02-12
Package Last Update: 2016-05-12
Language: PHP
License: MIT
Last Refreshed: 2024-11-30 15:18:14
Package Statistics
Total Downloads: 457
Monthly Downloads: 3
Daily Downloads: 0
Total Stars: 3
Total Watchers: 3
Total Forks: 1
Total Open Issues: 1

Laravel Likeable Plugin

This is a fork from rtconner/laravel-likeable by Robert Conner - smartersoftware.net , thank you for smart software :)

We have added option to have dislike as separate field (counter) or any other countable social reaction (follow,hate,dislike,like ..)

TODO:

Inspired by other laravel-likeable packages, will add options to tracks reaction with timestamp, which gives us more deep metrics etc..

Build Status Latest Stable Version License

Trait for Laravel Eloquent models to allow easy implementation of a "like" or "favorite" or "remember" feature.

Laravel 5 Documentation
Laravel 4 Documentation

Composer Install (for Laravel 5+)

composer require nanosolutions/laravel-likeable "~1.3"

Install and then run the migrations

'providers' => [
	\Nano\Likeable\LikeableServiceProvider::class,
],
php artisan vendor:publish --provider="Nano\Likeable\LikeableServiceProvider" --tag=migrations
php artisan migrate

Setup your models

use Nano\Likeable\Likeable;

class Article extends Model {
	use Likeable;
}

Sample Usage

$article->like(); // like the article for current user
$article->like($myUserId); // pass in your own user id
$article->like(0); // just add likes to the count, and don't track by user

$article->unlike(); // remove like from the article
$article->unlike($myUserId); // pass in your own user id
$article->unlike(0); // remove likes from the count -- does not check for user

// Dislike (new metric)
$article->like('dislike'); // alias -> $article->dislike();
$article->unlike('dislike'); // alias -> $article->dislike($myUserId);

// Or anything you want (no alias)
$article->like('follow');
$article->unlike('follow');


$article->likeCount; // get count of likes
$article->dislikeCount; // get count of dislikes

$article->likes; // Iterable Illuminate\Database\Eloquent\Collection of existing likes
$article->dislikes; // Iterable Illuminate\Database\Eloquent\Collection of existing disklikes
$article->liked(); // check if currently logged in user liked the article
$article->disliked(); // check if currently logged in user disliked the article
$article->liked($myUserId);

Article::whereLiked($myUserId) // find only articles where user liked them
	->with('likeCounter') // highly suggested to allow eager load
	->get();

Credits

  • Robert Conner - http://smartersoftware.net