我的所有项目都存在一个持续存在的问题,我需要帮助以了解如何让它发挥作用。
在我看来=
URL = mysite.com/product/40
所以这里的产品 ID 是 40
在 View 中,我正在执行一个 foreach 循环以显示拥有此产品的所有商家。我们有很多商家有很多产品,所以这是一个多对多的关系。
现在在我的 Controller 上
$product = Product::find($id); // to find the product
$users = $product->users; // here the user is the merchant that have the product
$store = Store::where('user_id', $user->id)->value('social');
这里我得到错误:
Trying to get property of non-object
所以我确实想访问 Controller 中每个商家的商店,我该怎么做?因为现在 $user
是一个集合。
最佳答案
请先使用 var_dump 验证商店是否正在提供对象。之后你可以看看 https://laravel.com/docs/5.6/queries了解更多详情。
关于php - 我对 foreach 理解 laravel 5.6 有疑问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52380483/