【问题标题】:Puling a specific item from a foreach loop in Laravel从 Laravel 的 foreach 循环中提取特定项目
【发布时间】:2018-02-27 18:14:56
【问题描述】:

我有一个包含 2 个表(类别和产品)的项目。他们有一对多的关系。许多产品都有一个类别。我正在提取特定类别的产品(连同其价格和描述)并将其存储在一个变量中,以便我可以在视图中使用。在视图中有一个 foreach 循环,我想显示特定类别的产品表中的每一行(带有价格和描述的产品)但它失败了,请帮忙?

前端控制器

public function itemOne() {
    //Get all phones(Have category of 1 ) in categories table
    $mobile = Category::find(1)
        ->products()
        ->whereNotNull("image")
        ->whereNotNull("name")
        ->whereNotNull("description")
        ->whereNotNull("price")
        ->whereNotNull("category_id")->get();

    return view('front.products')->withItems($mobile);
}

Products.blade.php

@foreach($items as $item)
                <img src="{{ asset('images/'.$item->image) }}">
    <br> {{  $item->name}} <br> {{ $item->price }}
@endforeach

【问题讨论】:

  • 您遇到的错误是什么,请提供。
  • @amitrawat 没有发生错误,所有第一个项目都只显示,但我希望 foreach 循环中的每个项目与前一个项目的显示方式及其具体价格和描述不同?

标签: php mysql laravel-5.4


【解决方案1】:
public function itemOne() {
    //Get all phones(Have category of 1 ) in categories table
    $mobile = Category::find(1)
        ->products()
        ->whereNotNull("image")
        ->whereNotNull("name")
        ->whereNotNull("description")
        ->whereNotNull("price")
        ->whereNotNull("category_id")->get();

    return view('front.products',compact('items'));
}

使用紧凑作为回报

【讨论】:

  • ,,, 您好,我已纠正,但问题仍然存在,,,foreach 循环中的单个项目正在不断重复,,但我需要每个项目唯一显示,而不与其一起重复价格和描述..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-19
  • 2016-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多