@mahmoudmonem if you want to check for an existing review, you'll also want to pass in the product_id
$findReview = Review::where(['user_id' => Auth::user()->id, 'product_id' => $request->product_id])->first();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have 3 tables Users | Products | Reviews
in my reviews table I have user_id and product_id
Once a user purchase specific product, he/she can submit review .. I am trying to prevent duplicated reviews for the same user. and I did the following to achieve that ..
1- in my "make_reviews_table.php"
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger('product_id');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->unique(['product_id', 'user_id']);
This actually prevents users from submitting a second review for the same product, but it throws a Database Error in the view and I don't want customers to see that .. so I tried to add this line in the controller [ even though I was so sure it's missing something
2- added conditional if before processing the review submission
public function store(Request $request)
{
$findReview = Review::where('user_id', Auth::user()->id)->first();
if($findReview) {
return back()->with('error', 'You already reviewed this product');
}else{
Review::create($request->all());
return redirect()->back()->with('success', 'Review Submitted Successfuly');
}
}
The problem here, which I knew, is that it prevents users from adding any other reviews on any other product. so I need to define "the current specific product" in the function. Or maybe there is something else better than this approach .. would appreciate your input.
Ps: my view url looks like .. website.com/{product-slug}
@mahmoudmonem if you want to check for an existing review, you'll also want to pass in the product_id
$findReview = Review::where(['user_id' => Auth::user()->id, 'product_id' => $request->product_id])->first();
Please or to participate in this conversation.