francescomalatesta / laravel-reactions

Implement reactions for your Eloquent models, easily.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Duplicates

haleyngonadi opened this issue · comments

Thank you for this! I'm having an issue where when a user reacts to a post, if they react again, instead of replacing the current reaction, a new record is created, with the exact same reaction!

Any way to fix this?

$reactable->reactions()->attach(

using sync() methood instead of attach()

this does not fix the issue

this does not fix the issue

Hey, maybe my solution will help you:

    $userId =   auth()->user()->id;
    $reaction = Reaction::findOrFail($request->react_id);
    $post = Post::where('id', $request->post_id)->first();

  if($post->reactions) {
    $check = $this->reactValidate($post->id, $userId);
    $alreadyReacted = $check[0]->count;
    $reactableID = $check[0]->rid;

    if ($alreadyReacted >= 1) {
              DB::table('reactables')->where('id', $reactableID)->delete();

    }

      auth()->user()->reactTo($post, $reaction);

The other bit:

   private function reactValidate($postid, $userID) {
      return DB::select('select r.id as rid, count(r.reaction_id) as count from reactables r
      where r.reactable_id ='. $postid .' and r.responder_id ='. $userID);
    }

this does not fix the issue

Hey, maybe my solution will help you:

    $userId =   auth()->user()->id;
    $reaction = Reaction::findOrFail($request->react_id);
    $post = Post::where('id', $request->post_id)->first();

  if($post->reactions) {
    $check = $this->reactValidate($post->id, $userId);
    $alreadyReacted = $check[0]->count;
    $reactableID = $check[0]->rid;

    if ($alreadyReacted >= 1) {
              DB::table('reactables')->where('id', $reactableID)->delete();

    }

      auth()->user()->reactTo($post, $reaction);

thank you for your answer, but it says that the react validate method does not exist