Patwan's avatar

Pulling Individual items from a Foreach loop in Laravel view

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
0 likes
2 replies
tisuchi's avatar

Have you tried with Eager loading with()?

2 likes
Patwan's avatar

Hello tisuchi,, I got the problem solved,, I used compact() ... Thanks for the help

Please or to participate in this conversation.