Skip to content

Implementation of a "like" or "favorite" or "remember" feature. Supports for session based favorites that are automatically transfered to an authenticated user when possible

License

Notifications You must be signed in to change notification settings

vaniyokk/laravel-favorites

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Favorites Plugin

Build Status Latest Stable Version License

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

About

Implementation of a "like" or "favorite" or "remember" feature. Supports for session based favorites that are automatically transfered to an authenticated user when possible

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages