2017-09-19 68 views
0

我有一个包含2个表格(类别和产品)的项目。他们有一对多的关系。许多产品都有一个类别。我拉动特定类别的产品(连同其价格和描述)并存储在一个变量中,以便我可以在视图中使用。在视图中我有一个foreach循环,我希望显示特定类别的产品表(每个产品的价格和说明)中的每一行,但它失败了,请协助?从Laravel的foreach循环中抽取特定项目

FrontController

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 
+0

什么是你正在请提供的错误。 –

+0

@amitrawat没有错误发生,所有第一项只显示,但我希望foreach循环中的每个项目显示不同于前一个,连同其特定的价格和描述? – Martin

回答

0
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')); 
} 

使用紧凑的回报

+0

,,,你好,我已经纠正,但问题仍然存在,,,在foreach循环中的单个项目不断重复,,,但我需要每个项目显示唯一没有与价格和描述一起重复.. – Martin