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?

Home Page:https://komarev.com/sources/laravel-love

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

seeding fails on: ReactionCounter for 'x' Reactant with ReactionType 'y' already exists.

jayenne opened this issue · comments

Hi, I've wrttien 3 seeders and randomly get a fail despite checking hasNotReactedTo

  • Users, Boards, Posts. a User can react to Users, Boards and Posts.
  • I run recount per model after seeding.
  • I am queueing on redis and using horizon
$actions = ['love','like','meh','dislike','hate'];
Post::inRandomOrder()
  ->where('user_id', '!=', $user->id)
  ->limit($count)
  ->each(function ($model) use ($user, $reacterFacade, $actions) {
      if ($reacterFacade->hasNotReactedTo($model)) {
          shuffle($actions);
          $reacterFacade->reactTo($model, $actions[0]);
      }
  });
// recount for this model only
\Artisan::call('love:recount', ['--model' => 'App\Models\Post']);

I think it might be to do with the hasNotReactedTo not knowing if a job exists for that reaction. does this make sence?
Thoughts on what I might be doing wrongly or ways to negate its effect and diying on seed?

ahh, could it be that my caling the recount isn't required ... and is actually causing the issue?

If you are creating reaction via facade you don't need to recount them. Counters will be incremented automatically.
What are you trying to achieve? Is it for test reasons?

It seems that you are re-using reacterFacade from the same user. You need to instantiate a new one each time for the user.

Try something like this:

$actions = ['love','like','meh','dislike','hate'];
Post::inRandomOrder()
  ->where('user_id', '!=', $user->id)
  ->limit($count)
  ->each(function ($model) use ($user, $actions) {
      $reacterFacade = $user->viaLoveReacter();
      if ($reacterFacade->hasNotReactedTo($model)) {
          shuffle($actions);
          $reacterFacade->reactTo($model, $actions[0]);
      }
  });

@jayenne I'm closing this issue. If your question is still actual feel free to continue conversation here

thank you Anton. sorry for my late response but you nailed it perfectly. ty.