尝试获取非对象的 属性 'Product_image'(视图:F:\softwares\xampp\htdocs\MyProject\resources\views\home.blade.php)
Trying to get property 'Product_image' of non-object (View: F:\softwares\xampp\htdocs\MyProject\resources\views\home.blade.php)
我正在尝试在 home.blade.php 视图中显示产品。它给了我 "Trying to get property 'Product_image' of non-object" 的错误。
这是我的 HomeController Index 方法
public function index()
{
$products = Product::pluck('name','id');
return view('home',[
'products' => $products,
]);
}
我的Home.blade.php如下
<div class="container">
<div class="row">
@foreach ($products as $product)
<div class="col-lg-4 col-md-4 col-sm-4 height" style="font-weight: bold;">
<div class="row">
<img src="{{ asset('/storage/'.$product->Product_image) }}" width="200px">
</div>
<div class="row">
<div class="col-lg-7 col-md-7 col-sm-7 height">
<div class="row" style="font-size: 30px;font-weight: bold">
{{ $product->name }}
</div>
<div class="row">
Description: {{ $product->description }}
</div>
<div class="row">
Price: {{ $product->price }}
</div>
<div class="row">
Category: {{ $product->category->name }}
</div>
<div class="row">
Sub-Category: {{ $product->subcategory->name }}
</div>
</div>
</div>
<div class="row">
<a class="btn btn-primary" href="#"> Add to Cart</a>
</div>
</div>
@endforeach
</div>
</div>
我如何解决这个问题?
在 HomeController Index 方法中,我将 Product::pluck('name','id') 更改为 Product::all();
public function index()
{
$products = Product::all();
return view('home',[
'products' => $products,
]);
}
我正在尝试在 home.blade.php 视图中显示产品。它给了我 "Trying to get property 'Product_image' of non-object" 的错误。 这是我的 HomeController Index 方法
public function index()
{
$products = Product::pluck('name','id');
return view('home',[
'products' => $products,
]);
}
我的Home.blade.php如下
<div class="container">
<div class="row">
@foreach ($products as $product)
<div class="col-lg-4 col-md-4 col-sm-4 height" style="font-weight: bold;">
<div class="row">
<img src="{{ asset('/storage/'.$product->Product_image) }}" width="200px">
</div>
<div class="row">
<div class="col-lg-7 col-md-7 col-sm-7 height">
<div class="row" style="font-size: 30px;font-weight: bold">
{{ $product->name }}
</div>
<div class="row">
Description: {{ $product->description }}
</div>
<div class="row">
Price: {{ $product->price }}
</div>
<div class="row">
Category: {{ $product->category->name }}
</div>
<div class="row">
Sub-Category: {{ $product->subcategory->name }}
</div>
</div>
</div>
<div class="row">
<a class="btn btn-primary" href="#"> Add to Cart</a>
</div>
</div>
@endforeach
</div>
</div>
我如何解决这个问题?
在 HomeController Index 方法中,我将 Product::pluck('name','id') 更改为 Product::all();
public function index()
{
$products = Product::all();
return view('home',[
'products' => $products,
]);
}