Allows October models to implement a 'favorite' or 'remember' or 'follow' feature. Note that this plugin ports the original christiankuri/laravel-favorite to October.
- Make sure to install the required
RainLab.User
plugin.
$ php artisan plugin:install RainLab.User
- Install this plugin from the backend or directly via artisan.
$ php artisan plugin:install Alxy.Favorites
Your models should import the Alxy.Favorites.Behaviors.Favoriteable
behavior. That will have the methods that you'll use to allow the model be favoriteable.
In all the examples I will use the Post model as the model that is 'Favoriteable', thats for example propuses only.
(see an example below):
use Model;
class Post extends Model
{
public $implement = ['Alxy.Favorites.Behaviors.Favoriteable'];
}
That's it ... your model is now "Favoriteable"!
The models can be favored with and without an authenticated user (see examples below):
If no param is passed to the favorite method, then the model will assume the authenticated user.
$post = Post::find(1);
$post->addFavorite(); // auth user is added to favorites of this post
$post->removeFavorite(); // auth user is removed from favorites of this post
$post->toggleFavorite(); // toggles the favorite status of this post
You can also pass a user model directly to these methods. In this case, the given user will be used.
$post = Post::find(1);
$user = RainLab\Models\User::first();
$post->addFavorite($user);
$post->removeFavorite($user);
$post->toggleFavorite($user);
The user model can also add to favorites and remove from favorites:
$user = User::first();
$post = Post::first();
$user->addFavorite($post);
$user->removeFavorite($post);
$user->toggleFavorite($post);
A user can return the objects he marked as favorite.
You just need to pass the class to the favoritesOf()
method of the User
model.
$user = Auth::getUser();
$user->favoritesOf(Post::class); // returns a collection of Posts the User has marked as favorite
To return the favorites count from an object, you just need to access the favorites_count
attribute from the model.
$post = Post::find(1);
$post->favorites_count; // returns the number of users that have marked as favorite this object.
To return the users who favored this object, you just need to call the favoritedBy()
method in the object.
$post = Post::find(1);
$post->favoritedBy(); // returns a collection with the Users that marked the post as favorite.
To return boolean value if the user has favorited the post object, you just need to call the isFavorited()
method in the object.
$post = Post::find(1);
$post->isFavorited(); // returns a boolean value if auth user has favorited the post.
Please report any issue you find in the issues page.
Pull requests are welcome.