@nicwek Change it to this
$productDetails = DB::table('products')->where('id', $request->product_id)->first();
So instead of get() use first() to get just one row and not collection.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am implementing a wish list for an ecommerce platform and i keep on getting this error, although everything works out pretty fine. How can i get rid of the error? Any input will be highly appreciated. Here's my code:
//My controller function for add to wishlist
public function addWishList(Request $request)
{
$wishlist = new Wishlist();
$wishlist->user_id = Auth::user()->id;
$wishlist->product_id = $request->product_id;
$wishlist->save();
$productDetails = DB::table('products')->where('id', $request->product_id)->get();
return view('product.detail', compact('productDetails'));
}
Here's part my view code:
<div class="tab-pane fade show active easyzoom easyzoom--overlay
easyzoom--with-thumbnails" i
d="sg1" role="tabpanel">
<a href="{{asset('storage/'.$productDetails->cover_img)}}">
<img src="{{asset('storage/'.$productDetails->cover_img)}}"
style="width: 300px;" alt="" class="img-fluid">
</a>
</div>
<div class="tab-pane" id="sg2" role="tabpanel">
<img src="{{asset('storage/'.$productDetails->alt_img)}}" alt="" class="img-fluid">
</div>
<div class="tab-pane" id="sg3" role="tabpanel">
<img src="{{asset('storage/'.$productDetails->alt_img2)}}" alt="" class="img-fluid">
</div>
<div class="tab-pane" id="sg4" role="tabpanel">
<img src="{{asset('storage/'.$productDetails->alt_img3)}}" alt="" class="img-fluid">
</div>
</div>
<div class="nav d-flex justify-content-between">
<a class="nav-item nav-link active" data-toggle="tab" href="#sg1">
<img src="{{asset('storage/'.$productDetails->cover_img)}}" alt=""></a>
<div class="pro-btns">
<a href="{{route('cart.add', $productDetails->id)}}" class="cart" id="CartButton"
name="CartButton">Add To Cart</a>
<br><br>
<?php
$wishlistData=DB::table('wish_list')- >rightJoin('products','wish_list.product_id','=','products.id')
->where('wish_list.product_id','=',$productDetails->id)->get();
$count=App\Wishlist::where(['product_id'=>
$productDetails->id])->count();
if($count=="0"){
?>
<form action="{{route('addToWishList')}}" method="post" role="form">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<input type="hidden" value="{{$productDetails->id}}" name="product_id">
<button type="submit" class="btn btn-default">Add to Wishlist</button>
</form>
<?php }else{?>
<h3 style="color:green">Added to Wishlist</h3><a href="{{url('/WishList')}}"
class="fav-com ml-2" data-toggle="tooltip"
<img src="{{ asset('images/it-fav.png') }}" alt=""></a>
<?php }?>
And what is this?
This was your default
//My controller function for add to wishlist
public function addWishList(Request $request)
{
$wishlist = new Wishlist();
$wishlist->user_id = Auth::user()->id;
$wishlist->product_id = $request->product_id;
$wishlist->save();
$productDetails = DB::table('products')->where('id', $request->product_id)->get();
return view('product.detail', compact('productDetails'));
}
You know that you have to change it to, also with reviews because you have reviews in the view.
$productDetails = Product::with('reviews')->find($request->product_id);
So I want to show me result of this
public function addWishList(Request $request)
{
//$wishlist = new Wishlist();
//$wishlist->user_id = Auth::user()->id;
//$wishlist->product_id = $request->product_id;
//$wishlist->save();
$productDetails = Product::with('reviews')->find($request->product_id);
dd($productDetails);
return view('product.detail', compact('productDetails'));
}
I just commented wishlist, because for dd it's not necessary.
Please or to participate in this conversation.