Level 70
Have you tried with Eager loading with()?
2 likes
I have a e-commerce project with 2 tables(Categories and products). They have One to Many relationship. Many products have one Category. Am pulling products of a specific category(together with its price and description) and storing in one variable so that I can use in the view. In the view am having a foreach loop whereby I want to display each row from the products table of a specific category(a product with its price and description) but it fails,,please assist?
FrontController
public function itemOne(){
//Get all phones(Have category of 1 ) in categories table
$mobile = Category::find(1)->products()->whereNotNull("image")->whereNotNull("name")->whereNotNull("description")->whereNotNull("price")->whereNotNull("category_id")->get();
return view('front.products')->withItems($mobile);
}
Products.blade.php
@foreach($items as $item)
<img src="{{ asset('images/'.$item->image) }}">
<br> {{ $item->name}} <br> {{ $item->price }}
@endforeach
Please or to participate in this conversation.