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

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

Closed jayenne closed 3 years ago

jayenne commented 3 years ago

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

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?

jayenne commented 3 years ago

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

antonkomarev commented 3 years ago

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?

antonkomarev commented 3 years ago

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]);
      }
  });
antonkomarev commented 3 years ago

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

jayenne commented 3 years ago

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