cybercog / laravel-love

Add Social Reactions to Laravel Eloquent Models. It lets people express how they feel about the content. Fully customizable Weighted Reaction System & Reaction Type System with Like, Dislike and any other custom emotion types. Do you react?
https://komarev.com/sources/laravel-love
MIT License
1.17k stars 71 forks source link

Add fetch reactions by Reacterable model #240

Closed antonkomarev closed 1 year ago

antonkomarev commented 1 year ago

Feature request appeared in issue:

This is implementation of the Solution №1 from the discussion:

High-level API (preferred)

Method \Cog\Contracts\Love\Reactant\Facades\Reactant::getReactionsBy method introduced:

public function getReactionsBy(
    Reacterable $reacterable,
): iterable;

Usage:

// Get all reactions the post received (API not changed)
$allReactionsOnPost = $post->viaLoveReactant()->getReactions();

// Get reactions the post received from the user
$userReactionsOnPost = $post->viaLoveReactant()->getReactionsBy($user);

Low-level API

Method \Cog\Contracts\Love\Reactant\Models\Reactant::getReactionsBy method introduced:

public function getReactionsBy(
    Reacter $reacter,
): iterable;

Usage:

// Get reactions the post received from the reacter user
$userReactionsOnPost = $post->getLoveReactant()->getReactionsBy($user->getLoveReacter());