我试图通过 Eloquent 关系在我的 Blade View 中传递日期,但出现上述错误。我一直无法找到与此案例类似的问题。这是我到目前为止所做的,我有以下模型
产品
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');
}
}
我像这样在我的 Controller 中传递数据
public function index()
{
$products = Product::with('prices')->orderBy('id')->get();
$prices = Price::all()->groupBy('date');
return view('home', compact('products', 'prices'));
}
这是我的 View 代码
@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
我错过了什么以及如何让我的代码工作?
最佳答案
例如将日期属性名称更新为 date_product。 让我知道它是否有效:)
关于mysql - 此集合实例上不存在属性 [日期],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45616448/