I am working on Laraval Framework.
Trying to fetch the product details by joining other tables.
3 Tables are there:
1. product
2. category
3. sub_category
Controller
class HomeController extends Controller
{
public function index()
{
$sliders = Slider::all();
$promotions = Promotion::all();
$categories = Categories::with('SubCategory')->get();
$products = Product::with('Category')->with('SubCategory')->orderBy('created_at','desc')->take(10)->get();
$hotdealproducts = HotDeal::with('Product')->get();
$featuredproducts = Product::with('Category')->with('SubCategory')->where('is_featured', 1)->orderBy('created_at','desc')->take(10)->get();
$testimonials = Testimonial::all();
return view('index', [
'sliders' => $sliders,
'promotions' => $promotions,
'categories' => $categories,
'products' => $products,
'hotdealproducts' => $hotdealproducts,
'featuredproducts' => $featuredproducts,
'testimonials' => $testimonials,
]);
// return $products;
}
}
JSON for $products
[{
"id":3,
"product_name":"Floral Print Buttoned",
"product_description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ",
"category":{
"id":1,
"category_name":"Clothing",
"icon":"fa fa-shopping-bag"
},
"sub_category":{
"id":1,
"category_id":1,
"sub_category":"Dresses"
},
"general_price":2000,
"offer_price":1500,
"cover_image":"1.jpg",
"is_featured":"1",
"created_at":null,
"updated_at":null
}]
In View, i am trying fetch the child JSON but getting error Invalid argument supplied for foreach()
@foreach ($products as $product)
.....
@foreach ($product->category as $category)
<a href="">
<img src="" alt="">
</a>
@endforeach
@endforeach
While using id is showing. Can someone please solve this problem, I checked all the stacks but I did not find any correct answer.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire