laravel-love icon indicating copy to clipboard operation
laravel-love copied to clipboard

would it be possible to expose the datetime of reactions

Open vesper8 opened this issue 4 years ago • 0 comments

This is a continuation of an idea started in https://github.com/cybercog/laravel-love/issues/181

I would find it extremely useful to be able to easily retrieve the reaction date when using scopeWhereReactedTo or scopeWhereReactedBy

I have users liking other users and I would like to be able to display to a user who has liked them and when specifically that like has been received

Right now those scopes return the users that have reacted to/by the specfied reaction type.. but I'm not seeing an easy way to also retrieve more details about that reaction, namely the datetime it occurred at

The following lends itself to a n+1 problem.. but this is the workaround I came up with to retrieve the raw reaction from the db table in order to expose the datetime of the reaction itself which is useful in my case

$rawReaction = (new Love)->getRawReaction($user, User::find(request('viaResourceId')), 'LIKE');
<?php

namespace App\Helpers;

use App\Models\LoveReaction;

class Love
{
    public function getRawReaction($reactable, $reacterable, $reactionType)
    {
        $rawReaction = LoveReaction::where('reactant_id', $reactable->love_reactant_id)
        ->where('reacter_id', $reacterable->love_reacter_id)
        ->where('reaction_type_id', config('love.reaction-types')[$reactionType])
        ->first();

        return $rawReaction;
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class LoveReaction extends Model
{
    protected $table = 'love_reactions';
}

vesper8 avatar Jan 28 '21 22:01 vesper8