[英]Undefined variable: $ Laravel 5.8
這是youthController的代碼:
public function index()
{
$firstProducts=Product::where('publication_status',1)
->take(4)
->get();
return View('front-end.home.home',[
'$firstProducts'=>$firstProducts
]);
}
這是我寫到home.blade.php的代碼
@foreach($firstProducts as $firstProduct)
<div class="col-md-3 col-md2 animated wow fadeInRight" data-wow-delay=".5s">
<div class="col-md1 simpleCart_shelfItem">
<a href="single.html">
<img class="img-responsive" src="{{asset($firstProduct->product_image)}}" alt="" />
</a>
<h3><a href="single.html">{{$firstProduct->product_name}}</a></h3>
<div class="price">
<h5 class="item_price">TK.{{$firstProduct->product_price}}</h5>
<a href="#" class="item_add">Add To Cart</a>
<div class="clearfix"> </div>
</div>
</div>
</div>
@endforeach
我收到此錯誤:
ErrorException(E_ERROR)未定義的變量:firstProducts(視圖:C:\\ xampp \\ htdocs \\ project-youth \\ resources \\ views \\ front \\ end \\ home \\ home.blade.php)先前的異常未定義的變量:firstProducts(0)
如何解決這個問題呢?
采用
return view('front-end.home.home', ['firstProducts'=> $firstProducts]);
要么
return view('front-end.home.home', compact('firstProducts'));
您不需要使用$
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.