You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel报错:Property [slug]不存在于集合实例,请求排查

Fix: Property [slug] does not exist on this collection instance

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 show method in ProductsController correctly fetches a single product using first(), so it will pass a single instance to the products.show view.
  • Your route definition for products.show is properly set up to accept the slug parameter.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:27:29