【问题标题】:I have a problem with foreach understanding laravel 5.6我对 foreach 理解 laravel 5.6 有疑问
【发布时间】:2018-09-18 16:02:41
【问题描述】:

我的所有项目都有一个长期存在的问题,我需要帮助来了解如何使它工作。

在我看来 =

URL = mysite.com/product/40 所以这里的产品 ID 是 40

在视图中,我正在执行一个 foreach 循环来显示所有拥有此产品的商家。我们有很多商家有很多产品,所以这是多对多的关系。

现在在我的控制器上

$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');

这里我得到错误:

试图获取非对象的属性

所以我确实想访问控制器中每个商家的商店,我该怎么做?因为现在$user 是一个集合。

【问题讨论】:

  • 你转储变量 $user
  • $product = Product::where('id',$id)->get()
  • 如果 product 只有 1 个 user,则更改关系以获取用户实例而不是集合
  • 转储变量 $user 返回 null ,甚至 $product->users 返回 null .. 奇怪,因为当我在视图中发布 @foreach ($package->users as $merchant) 时,它返回我所有的用户
  • 您的关系设置正确吗?如果一个产品有很多用户,它应该是$product->users,你可以使用foreach循环。

标签: php mysql laravel


【解决方案1】:

请首先使用 var_dump 验证商店是否提供对象。之后您可以查看https://laravel.com/docs/5.6/queries 了解更多详情。

【讨论】:

  • 对不起,这个链接对我没有帮助,$product->users 给了我一个用户列表,我如何一个一个地访问这些用户中的一个。因为在视图上的 foreach 循环中,我需要他们在卡片上显示一些内容
【解决方案2】:

1) 首先你可以使用 Injection 来避免这条线:$product = Product::find($id);

public function your_controller_methon(Product $product) {}

Laravel 会自动为你解决问题,并且 $product 已经包含 Product 对象。

2)如果你有关系,你应该做类似的事情:

$product->stores - 检索在 product_id 列中包含特定产品的所有商店。你可以这样做:$product->stores()->pluck('social'); 从所有拥有特定产品的商家那里检索社交列表。

关于你可以在这里阅读的关系:https://laravel.com/docs/5.7/eloquent-relationships

【讨论】:

    【解决方案3】:

    您可以重构代码以使用 whereIn() 查询构建器方法,因为您有很多用户使用一个产品。你会得到类似的东西:

    $product = Product::find($id);  // to find the product
    
    $users = $product->users->pluck('id');
    $stores = Store::whereIn('user_id', $users->all())->value('social');
    

    这意味着您的 $stores 变量将包含用户拥有的那些商店。

    PS:一定要检查$users是否为空或null,以免遇到意外错误

    【讨论】:

    • 不工作:/它为所有商家显​​示相同的数据
    • 很奇怪,因为您的代码描述的过程是 one product->users of the product->the stores that those users belong 这意味着如果另一个产品具有这些 users 他们将获得相同数量的商店。这不是你的本意吗? @阿里姆
    • 所以我可以制作另一个场景让您更好地理解。可以说我有很多诊所。现在我在诊所 id 1 的页面上,所以在那个页面上我显示了所有的工作人员。所以我使用 foreach ($staffs as $staff) ...STAFF AREA ... endforeach 。好的,现在在员工区,我想显示一个日历,如果我想在 mysite.com/calendar/{user_id} 上显示它。我只需要在页面上显示 {{calendar}} 作为变量...因为它知道我正在显示 {user_id} 的日历,但是当我尝试在牙医列表上执行此操作时,我不能因为它不会找到 id
    • 我不能只把日历功能放在视图上,它是一个 php 的 loooot ,它上面有很多功能,不能在同一页面上重新声明它们。
    【解决方案4】:

    根据您的代码,这里的 $user 是一个值,而不是一个集合。

    变化:

    $store = Store::where('user_id', $user->id)->value('social');
    

    $store = Store::where('user_id', $user);
    

    它会起作用的。

    要将 $user 设为集合,请执行这样的查询,以便它返回数组,例如:

    $product = Product::find($id); 
    $user = Product::where('user', $product->user)->get();
    

    这将返回该产品的用户集合。

    然后执行foreach循环:

    foreach($user as $rowdata){
        $store = Store::where('user_id', $rowdata->id)->get();
    }
    

    【讨论】:

    • 你确定这是正确的吗? $user = Product::where('user', $product->user)->get(); 您将整个集合对象作为第二个参数传递给 where() 函数
    • 在 eloquent 中,当你使用 find() 时,它总是返回一行,而不是一个集合。
    • 哦,谢谢。我完全没有注意到这一点。但是,这意味着它将一个完整的模型对象作为第二个参数传递。你觉得这样行吗?
    • 不,它只是传递产品对象的用户值。
    • 问题是我不能在控制器内部使用foreach,因为它是一个很长很长的函数,并且函数中有其他函数,我得到错误代码,无法重新声明函数名......等等......
    【解决方案5】:

    你应该试试这个:

    $product = Product::find($id); 
    $user = Product::where('user', $product->user)->get();
    
    foreach($user as $rowdata){
        $store = Store::where('user_id', $rowdata->id)->get();
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 2020-12-30
      • 2017-02-22
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      相关资源
      最近更新 更多