php - 我对 foreach 理解 laravel 5.6 有疑问

标签 php mysql laravel

我的所有项目都存在一个持续存在的问题,我需要帮助以了解如何让它发挥作用。

在我看来=

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/

相关文章:

mysql - 我需要选择时间戳并插入日期

MySQL InnoDB : A long semaphore wait:

Laravel - 保护 API 路由

php - 传递 API 数据以查看 Laravel

PHP 写入文件 - 权限被拒绝

phpmyadmin Designer Tab 不同颜色的含义?

php - fatal error : Uncaught ArgumentCountError: Too few arguments to function

php - PHP-显示错误的数组

java - XAMPP jdbc 驱动程序不加载

laravel - 如何在 Laravel 中获取有关登录凭据失败的自定义消息?