0

I'm so confused, I was trying to fetch data from my database using laravel 8 and I'm sure I defined the variable right but it's having errors.

this is my controller:

public function homepage(){
    $product_display = DB::table('products')->get();
    return view('customer.homepage', compact('product_display'));
}

And this is the blade file:

<div class="content-wrapper pt-4">
  <section class="content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-lg-3">
          <div class="card border border-danger">
            <div class="card-body">
            </div>
          </div>
        </div>
        <div class="col-lg-9">
          <div class="card border border-danger">
            <div class="card-body">
            @foreach($product_display as $pd)
              <div class="card">
                <h3>{{ $pd->prod_name }}</h3>
              </div>
            @endforeach
            </div>
          </div>
        </div>
      </div>
    </div>
  </section>
</div>

it says, product_display is not defined.

Teemu
  • 22,918
  • 7
  • 53
  • 106
JDD
  • 1
  • 3
  • Yup, see https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and How are the tags related to the question, this seems to be purely a PHP issue ..? – Teemu Jun 29 '21 at 15:02

1 Answers1

0

Use @dd($pd) in Blade foreach loop. You will see if the variable defined or not.

DB::get() returns a stdClass so maybe tryto use view('customer.homepage')->with(['product_display' => $product_display]) instead

tmkzmu
  • 11
  • 2