Duplicates
haleyngonadi opened this issue · comments
haley. commented
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?
Nguyễn Trần Chung commented
laravel-reactions/src/Traits/Reacts.php
Line 12 in 5de9957
using sync() methood instead of attach()
Digitalcoaster commented
this does not fix the issue
haley. commented
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);
}
Digitalcoaster commented
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