On my website, users can upvote or downvote posts.
SubmissionVote::where('submission_id', $submission->id)->where('user_id', Auth::user()->id)->delete();
$submissionVote = new SubmissionVote;
$submissionVote->submission_id = $submission->id;
$submissionVote->user_id = Auth::user()->id;
$submissionVote->vote = $vote;
So if a user changes their vote, I delete the old vote (without checking if there was an old vote because it's less queries) before recording the new vote.
However, I've seen that you can attach a "unique" parameter to migrations. Something like this.
$table->unique(["submission_id", "user_id"]);
Would this work? And if so, would it make the delete query redundant? Also, if I did it this way would it return an error if there already exists a record (I don't want that)?