Laravel报错:Property [slug]不存在于集合实例,请求排查
Hey there, let's sort out this error quickly! The issue comes down to a small variable mix-up in your Blade template.
What's causing the error?
In your index.blade.php, you're trying to access $products->slug to generate the route. But $products is a collection of products (a group of items), while each individual product in your loop is stored in the $product variable. Collections don't have a slug property—only single product instances do. That's why Laravel is throwing that error.
The Fix
Update the route link in your index.blade.php from this:
<a class="ps-shoe__overlay" href="{{ route('products.show', $products->slug) }}"></a>
To this:
<a class="ps-shoe__overlay" href="{{ route('products.show', $product->slug) }}"></a>
Double-checking other parts (they look good!)
Just to confirm, your other code pieces are correct:
- Your
showmethod inProductsControllercorrectly fetches a single product usingfirst(), so it will pass a single instance to theproducts.showview. - Your route definition for
products.showis properly set up to accept theslugparameter.
Once you make that small change, the link should generate correctly and you'll be able to access the product details page without errors.
内容的提问来源于stack exchange,提问作者Firas Boukhriss

