618

Product support

Get help in the plugin support forum.

Categories

October Favorites Plugin

Allows October models to implement a 'favorite' or 'remember' or 'follow' feature. Note that this plugin ports the original christiankuri/laravel-favorite to October.

This is a developer plugin and comes with no user interface. It adds a model behaviour to use with your models and a programmer friendly API.

A quick example of the plugin's capacities:

$post = Post::find(1);
$post->addFavorite(); // auth user added to favorites this post
$post->removeFavorite(); // auth user removed from favorites this post
$post->toggleFavorite(); // auth user toggles the favorite status from this post

Installation

1) Make sure to install the required RainLab.User plugin.

$ php artisan plugin:install RainLab.User

2) Install this plugin from the backend or directly via artisan.

$ php artisan plugin:install Alxy.Favorites

Models

Your models should implement 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"!

Usage

The models can be favored with and without an authenticated user (see examples below):

Add to favorites and remove from favorites:

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 added to favorites this post
$post->removeFavorite(); // auth user removed from favorites this post
$post->toggleFavorite(); // auth user toggles the favorite status from 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); // user with that id added to favorites this post
$post->removeFavorite($user); // user with that id removed from favorites this post
$post->toggleFavorite($user); // user with that id toggles the favorite status from this post

The user model can also add to favorites and remove from favorites:

$user = User::first();
$post = Post::first();
$user->addFavorite($post); // The user added to favorites this post
$user->removeFavorite($post); // The user removed from favorites this post
$user->toggleFavorite($post); // The user toggles the favorite status from this post

Return the favorite objects for the user:

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

Return the favoirites count from an object:

You can 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.

Return the users who marked this object as favorite

You can return the users who marked 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.
1.0.1

First version of Favorites

Nov 19, 2017