Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nicwek's avatar

Property [cover_img] does not exist on this collection instance

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 }?>
0 likes
30 replies
MichalOravec's avatar

@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.

nicwek's avatar

Thanks, @michaloravec, for the quick response. However, I just tried to implement it your way and am getting additional errors and this time, no item is being added to the wishlist.

Call to undefined method stdClass::getStarRating()
IlijaTatalovic's avatar

What do you want to get as a database query result in $productDetails variable? An Eloquent collection or a Product model? Try to dodd($productDetails) and show us what you get.`

$productDetails = DB::table('products')->where('id', $request->product_id)->get();
dd($productDetails)
nicwek's avatar

It's part of the blade file posted earlier but I excepted it at first.

nicwek's avatar

Heres my result for the dump and die:

Illuminate\Support\Collection {#893 ▼
   #items: array:1 [▼
  0 => {#888 ▼
  +"id": 13
  +"name": "Power Grinder"
  +"details": "Bosch Brand New Grinder"
  +"description": "The Bosch 1375A 4-1/2 In. Angle Grinder features a powerful 6.0 Amp motor that produces 			 
  11,000 no-load RPM, making this compact grinder a powerhouse tool."
  +"brand": "Bosch"
  +"is_available": "Available"
  +"price": 7500.0
  +"cover_img": "products\May2020\mCc1DdkJ4T5vpOuIn9Bl.jpg"
  +"alt_img": "products\May2020\XLtcc2LcAndYy1N8M8I6.jpg"
  +"alt_img2": "products\May2020\WDEMig3lm40jPGjgOaaG.jpg"
  +"alt_img3": "products\May2020\ZlDbdRNw6FQY984mrZzT.jpg"
  +"shop_id": 21
  +"created_at": "2020-05-09 13:03:00"
  +"updated_at": "2020-05-11 13:08:50"
}

] }

For the blade file:

                            <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>
                            <a class="nav-item nav-link" data-toggle="tab" href="#sg2"><img src="{{asset('storage/'.$productDetails->alt_img)}}" alt=""></a>
                            <a class="nav-item nav-link" data-toggle="tab" href="#sg3"><img src="{{asset('storage/'.$productDetails->alt_img2)}}" alt=""></a>
                            <a class="nav-item nav-link" data-toggle="tab" href="#sg4"><img src="{{asset('storage/'.$productDetails->alt_img3)}}" alt=""></a>
                        </div>
                    </div>
                </div>
                <div class="col-md-6 ml-10">
                    <div class="sg-content">
                        <div class="pro-tag">
                            <ul class="list-unstyled list-inline">
                               <!-- <li class="list-inline-item"><a href="">Home Appliance ,</a></li>
                                <li class="list-inline-item"><a href="">Smart Led Tv</a></li> -->
                            </ul>
                        </div>
                         <div class="pro-name">
                             <p>{{$productDetails->name}}</p>
                         </div>
                         <div class="pro-rating">
                             <ul class="list-unstyled list-inline">
                                 <li class="list-inline-item"><i class="fa fa-star"></i></li>
                                 <li class="list-inline-item"><i class="fa fa-star"></i></li>
                                 <li class="list-inline-item"><i class="fa fa-star"></i></li>
                                 <li class="list-inline-item"><i class="fa fa-star"></i></li>
                                 <li class="list-inline-item"><i class="fa fa-star-o"></i></li>
                                 <li class="list-inline-item"><a href="">( Review )</a></li>
                             </ul>
                         </div>
                         <div class="pro-price">
                             <ul class=" list-inline">
                                 <li class="list-inline-item">KES {{$productDetails->price}}</li>
                                 <li class="list-inline-item" style="display: none">KES {{$productDetails->price}}</li>
        
                             </ul>
                             <p>Availability : <span>{{$productDetails->is_available}}</span> </p>
                         </div>
                         <div class="colo-siz">
                             
                             
                             
                             <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" data-placement="top" title="View Your Wishlist"><img src="{{ asset('images/it-fav.png') }}" alt=""></a>
                              <?php }?>








    









                                 


                                  
                                  <a href="" class="fav-com"  data-toggle="tooltip" data-placement="top" title="Compare"><img src="{{ asset('images/it-comp.png') }}" alt=""></a>
                                  <button class="btn btn-primary py-2 ml-1" style="border-radius: 17px"    onclick="window.location.href = '{{url('quote/'.$productDetails->id)}}';">Request for Quote</button>
                             </div>
                             
                         </div>
                    </div>
                </div>
                <div class="col-md-12">
                    <div class="sg-tab">
                        <ul class="nav nav-tabs" role="tablist">
                            <li class="nav-item"><a class="nav-link active" data-toggle="tab" href="#pro-det">Product Details</a></li>
                            <li class="nav-item"><a class="nav-link" data-toggle="tab" href="#company">Company Profile</a></li>

                            <li class="nav-item"><a class="nav-link" data-toggle="tab" href="#rev">Reviews</a></li>
                        </ul>
                        <div class="tab-content">
                            <div class="tab-pane fade show active" id="pro-det" role="tabpanel">
                                <p>{{$productDetails->description}}</p>
                            </div>

                            <div class="tab-pane fade" id="company" role="tabpanel">
                                <p>{{$productDetails->description}}</p>
                            </div>


                            <div class="tab-pane fade" id="rev" role="tabpanel">
                                
                                <div class="review-form">
                                    <h6>Add Your Review</h6>
                                <form action="{{route('review.store')}}" method="POST">
                                    @csrf
                                        <div class="row">
                                            <div class="col-md-12">
                                                <div class="star-rating">
                                                    <label>Your Rating*</label>                                                       
                                                    <input type="text" name="rating" class="rating-value">
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <label for="headline">Headline</label>

                                                


                                                <star-rating :rating="{{$productDetails->getStarRating()}}"></star-rating>

                                                <input type="text" name="headline">
                                            </div>
                                            <div class="col-md-6">
                                            <input type="hidden" name="product_id" value="{{$productDetails->id}}">
                                            </div>

                                            <div class="col-md-12">
                                                <label>Your Review*</label>
                                                <textarea required="" name="description"></textarea>
                                                <button type="submit">Add Review</button>
                                            </div>
                                            <div class="col-md-6">
                                                <h4 class="py-5"><u>Reviews</u></h4>
                                                <ul>
                                                    @forelse ($productDetails->reviews as $review)

                                                <li>{{$review->headline}}</li>
                                                        
                                                    @empty
                                                        
                                                    @endforelse
                                                </ul>

                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
MichalOravec's avatar

How I said before you have to use first() insetad of get(), because you dump a result and there is still a collection.

$productDetails = DB::table('products')->where('id', $request->product_id)->first();

Also post whole your Product model, I want to know what exactly are you doing inside of getStarRating() method.

nicwek's avatar

Actually, the getStarRating() is non functional in the code so i removed it and the error cleared out.

So now i get this new error:

Undefined property: stdClass::$reviews

my reviews controller is:

public function store(Request $request)
{
    auth()->user()->review()->create($request->all());

}

with the blade having:

              <ul>
                 @forelse ($productDetails->reviews as $review)

                  <li>{{$review->headline}}</li>
                                                        
                 @empty
                                                        
                 @endforelse
                  </ul>
IlijaTatalovic's avatar

@michaloravec is right. You shoul use ->first() because you do not need collection. But if you do not want to change that, than you should change in your blade the way how you acces to $productDetails properties. Try like this for every property, not just for cover_img

$productDetails[0]->cover_img
MichalOravec's avatar

@nicwek Does your Product model relationship reviews?

$productDetails = Product::with('reviews')->find($request->product_id);
nicwek's avatar

@michaloravec, yes it does. Heres, the relationship:

 public function reviews()
{
    return $this->hasMany(ProductReview::class);
}
nicwek's avatar

Kindly explain to me what you are doing here:

$productDetails = Product::with('reviews')->find($request->product_id);
MichalOravec's avatar

This code

$productDetails = Product::with('reviews')->find($request->product_id);

is same like

$productDetails = DB::table('products')->with('reviews')->where('id', $request->product_id)->first();

Documentation: https://laravel.com/docs/7.x/eloquent#retrieving-single-models

You want to retrieve a model by id, so instead of ->where('id', $request->product_id)->first(); you cand do simple ->find($request->product_id);

nicwek's avatar

I don't know what am doing wrong, still no breakthroughs.

nicwek's avatar

I am still getting this as the error:

Undefined property: stdClass::$reviews
MichalOravec's avatar

@nicwek Post your whole User, Product and Review models. And also migrations for those models.

nicwek's avatar

Here are the models:

User Model:

class Product extends Model
{
public function shop()
{
    return $this->belongsTo('App\Shop', 'shop_id');
}

public function reviews()
{
    return $this->hasMany(ProductReview::class);
}

}

ProductReviews Model:

class ProductReview extends Model
{
protected $fillable = [
    'headline', 'description', 'rating', 'product_id',
];
}

Product Model:

class Product extends Model
{
public function shop()
{
    return $this->belongsTo('App\Shop', 'shop_id');
}

public function reviews()
{
    return $this->hasMany(ProductReview::class);
}

}
MichalOravec's avatar

@nicwek When you dump this, what do you get?

$productDetails = Product::with('reviews')->find($request->product_id);

dd($productDetails);
nicwek's avatar

Honestly, i have no controller where i have created a variable $productDetails in relation to reviews.

Should i have this $productDetails in ProductController or any controller by any chance?

MichalOravec's avatar
Level 75

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.

nicwek's avatar

Ok, now i get you. So heres the result:

 App\Product {#879 ▼
 #connection: "mysql"
 #table: "products"
 #primaryKey: "id"
 #keyType: "int"
+incrementing: true
 #with: []
 #withCount: []
 #perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:14 [▼
"id" => 13
"name" => "Power Grinder"
"details" => "Bosch Brand New Grinder"
"description" => "The Bosch 1375A 4-1/2 In. Angle Grinder features a powerful 
 6.0 Amp motor that produces 
11,000 no-load RPM, making this compact grinder a powerhouse tool."
"brand" => "Bosch"
"is_available" => "Available"
"price" => 7500.0
"cover_img" => "products\May2020\mCc1DdkJ4T5vpOuIn9Bl.jpg"
"alt_img" => "products\May2020\XLtcc2LcAndYy1N8M8I6.jpg"
"alt_img2" => "products\May2020\WDEMig3lm40jPGjgOaaG.jpg"
"alt_img3" => "products\May2020\ZlDbdRNw6FQY984mrZzT.jpg"
"shop_id" => 21
"created_at" => "2020-05-09 13:03:00"
"updated_at" => "2020-05-11 13:08:50"
 ]
 #original: array:14 [▼
"id" => 13
"name" => "Power Grinder"
"details" => "Bosch Brand New Grinder"
"description" => "The Bosch 1375A 4-1/2 In. Angle Grinder features a powerful 6.0 Amp motor that produces 11,000 no-load RPM, making this compact grinder a powerhouse tool."
"brand" => "Bosch"
"is_available" => "Available"
"price" => 7500.0
"cover_img" => "products\May2020\mCc1DdkJ4T5vpOuIn9Bl.jpg"
"alt_img" => "products\May2020\XLtcc2LcAndYy1N8M8I6.jpg"
"alt_img2" => "products\May2020\WDEMig3lm40jPGjgOaaG.jpg"
"alt_img3" => "products\May2020\ZlDbdRNw6FQY984mrZzT.jpg"
"shop_id" => 21
"created_at" => "2020-05-09 13:03:00"
"updated_at" => "2020-05-11 13:08:50"
 ]
 #changes: []
 #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
   #dispatchesEvents: []
  #observables: []
   #relations: array:1 [▶]
   #touches: []
   +timestamps: true
  #hidden: []
   #visible: []
   #fillable: []
    #guarded: array:1 [▼
  0 => "*"
 ]
MichalOravec's avatar

And when you try this

$productDetails = Product::with('reviews')->find($request->product_id);

dd($productDetails->reviews);
nicwek's avatar

This has actually done the trick :-)

 $productDetails = Product::with('reviews')->find($request->product_id);

I am sincerely very grateful for all your input. @michaloravec

nicwek's avatar

Sorry about that. However, I got to understand what I was doing wrong so thanks a lot.

Please or to participate in this conversation.