514

Product support

Visit this product's website for support.

  • Added on Jul 8, 2018
  • Current version: 1.0.5
  • Platform Compatibility
    v3.x not tested
    v2.x not tested
    v1.x use latest
  • Created by

Categories

Favorite Button Plugin

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

Index

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 BABA.Favoritebutton

Models

Your models should import the BABA.Favoritebutton.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 = ['BABA.Favoritebutton.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):

Component:

{% component 'favoriteButton' post_id=post.id %}
{% component 'myFavoritePosts' %}

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 favorites 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.

Security

Please report any issue you find in the issues page.
Pull requests are welcome.

Credits

1.0.5

add post users component

Nov 20, 2018

1.0.4

add views

Nov 20, 2018

1.0.3

Fix the problem that the post id is incorrect

Aug 11, 2018

1.0.2

show favorites count

Aug 10, 2018

1.0.1

Initialize plugin.

Jul 03, 2018