sugar-agency / laravel-favorites
Implementation of a 'like' or 'favorite' or 'remember' feature. Supports for session based favorites that are automatically transfered to an authenticated user when possible
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 1 057
Dependents: 0
Suggesters: 0
Security: 0
Stars: 9
Watchers: 2
Forks: 4
Open Issues: 2
Requires
- php: >=5.5.0
- illuminate/database: >=5.0
- illuminate/support: >=5.0
Requires (Dev)
- mockery/mockery: ~0.9
- orchestra/testbench: ~3.0
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2021-01-08 22:54:19 UTC
README
Trait for Laravel Eloquent models to allow easy implementation of a "like" or "favorite" or "remember" feature.
Based heavily on rtconner/laravel-likeable.
Composer Install
composer require sugar/laravel-favorites
Install and then run the migrations
'providers' => [ \Sugar\Favorites\FavoritesServiceProvider::class, ],
php artisan vendor:publish --provider="Sugar\Favorites\FavoritesServiceProvider"
php artisan migrate
Setup your models
class Article extends \Illuminate\Database\Eloquent\Model { use \Sugar\Favorites\Likeable; }
Configuration file
After installation, the config file is located at config/favorites.php
You can :
- enable session fallback for likes
- define the lifetime (in minutes) for the command favorites:clean
- define if the command favorites:clean should delete only session likes
Likeable Trait
Like model
$article->like(); // like the article for current user / session $article->like($myUserId, $false); // like the article for specific user
Unlike model
$article->unlike(); // like the article for current user / session $article->unlike($myUserId, $false); // unlike the article for specific user
Like count
$article->likeCount; // get count of likes $article->getLikeCountByDate('2012-01-30'); // Count likes for a specific date $article->getLikeCountByDate('2012-01-30', '2016-01-30'); // Count likes for a date range
Check if Model is currently liked
$article->liked(); // check if currently logged in user or session user liked the article $article->liked($myUserId, $false);
Get collection of existing likes
$article->likes; // Iterable Illuminate\Database\Eloquent\Collection of existing likes
Find only articles where user liked them
Article::whereLikedBy()->get(); // for the current user / session Article::whereLikedBy($myUserId, false)->get(); // for a specific user
Command
Delete likes after a certain amount of time
Because someone's favorite could be 'outdated' and not representative anymore.
There is a command for that. The time after which a like is outdated can be set in the config, also you can define if it should only delete session likes
php artisan favorites:clean
Helper class
In order to convert session likes to user likes (for example after a visitor registers on you site) you need to use the Helper class.
This workaround is nescessary because session id's are regenerated after login/logout.
class AuthController extends Controller { use AuthenticatesAndRegistersUsers { login as traitlogin; register as traitregister; } use ThrottlesLogins; public function login(Request $request, \Sugar\Favorites\Helper $helper) { $session_id = $helper->sessionId(); $return = $this->traitlogin($request); if(Auth::check()){ $helper->convertSessionToUserLikes($session_id, Auth::user()->id); } return $return; } public function register(Request $request, \Sugar\Favorites\Helper $helper){ $session_id = $helper->sessionId(); $return = $this->traitregister($request); if(Auth::check()){ $helper->convertSessionToUserLikes($session_id, Auth::user()->id); } return $return; } ... }
Credits
- Gregory Claeyssens - http://sugar.gent
- Robert Conner - http://smartersoftware.net