interactions() ->where('user_id', $user->id) ->where('type', InteractionType::Like) ->first(); // Delete the existing like if exists if ($existingLike) { $existingLike->delete(); return response()->json(['message' => 'Successfully unliked the post.']); } // Else, create a new like $data = [ 'type' => InteractionType::Like, 'user_id' => Auth::user()->id, ]; Deal::unguard(); $deal->interactions()->create($data); Deal::reguard(); } catch (\Throwable $e) { Log::error('Error when liked a deal: id'.$deal->id.$e->getMessage(), $e->getTrace()); return response()->json(['error' => 'Something went wrong.'], 500); } return response()->json(['message' => 'Successfully liked the post.']); } }