我試圖從一個雄辯的關係中通過我的刀片視圖中的日期,但得到上述錯誤。我一直無法找到類似於這種情況的問題。這是我迄今所做的,我有以下型號此收集實例中不存在屬性[日期]
產品
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
我錯過了什麼,如何讓我的代碼工作?
你有你的價格模型的日期屬性? – unreleased
是的,我這樣做,價格表列是id,product_id,日期和成本 – Mena