1
我想在我的视图中显示雄辩关系中的数据,但我似乎做错了什么。这里是我的模型关系,控制器和视图无法显示与laravel的雄辩关系的数据5.4
产品
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()
{
$this->belongsTo(Product::class);
}
}
的HomeController
public function index()
{
$products = Product::with('prices')->get();
return view('home', compact('products'));
}
查看
@foreach ($products as $product)
<tr>
<td>{{ $product->prices->date }}</td>
<td>{{ ucwords($product->name) }}</td>
<td>{{ $product->prices->cost }}</td>
</tr>
@endforeach
我的观点返回错误 Property [date] does not exist on this collection instance
我该如何解决此错误?
哇!从来不知道它是这样的。问题解决了,谢谢。 – Mena
很高兴知道它的作品!你能接受答案是否正确?谢谢! – Laerte