2017-08-10 63 views
1

我試圖從一個雄辯的關係中通過我的刀片視圖中的日期,但得到上述錯誤。我一直無法找到類似於這種情況的問題。這是我迄今所做的,我有以下型號此收集實例中不存在屬性[日期]

產品

class Product extends Model 
{ 
protected $guarded = ['id']; 

public function prices() 
{ 
    return $this->hasMany(Price::class, 'product_id'); 
} 
} 

價格

class Price extends Model 
{ 
protected $guarded = ['id']; 

public function product() 
{ 
    return $this->belongsTo(Product::class, 'product_id'); 
} 
} 

我傳遞數據在我的控制器這樣

public function index() 
{ 
$products = Product::with('prices')->orderBy('id')->get(); 

$prices = Price::all()->groupBy('date'); 

return view('home', compact('products', 'prices')); 
} 

而我的這是我的觀點代碼

@foreach($prices as $price) 
<div class="panel panel-default"> 
    <div class="panel-heading"> 
     {{ $price->date }} 
    </div> 

    <table class="table table-striped"> 
     <thead> 
      <tr> 
       <th>Product</th> 
       <th>Price</th> 
       <th>Difference</th> 
       <th>Percentage</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
      @foreach($price->product as $product)  
       <td>{{ $product->name }}</td> 
      @endforeach 
       <td>{{ $price->cost }}</td> 
       <td>0</td> 
       <td>0</td> 
      </tr> 
     </tbody> 
    </table> 
</div> 
@endforeach 

我錯過了什麼,如何讓我的代碼工作?

+0

你有你的價格模型的日期屬性? – unreleased

+0

是的,我這樣做,價格表列是id,product_id,日期和成本 – Mena

回答

0

例如,將日期屬性名稱更新爲date_product。 讓我知道它是否工作:)

+0

不幸的是,它沒有,返回相同的錯誤。 – Mena

+0

Put console error message – BKF

+0

'(2/2)ErrorException 此收集實例中不存在屬性[created_at]。 (查看:E:\ wamp64 \ www \ ndcl \ resources \ views \ home.blade.php)' – Mena

相關問題