You mean $product->productNam not $product->$productNam
Undefined variable in foreach in view.
Hello, I'm pretty new to laravel and i was experimenting with some basic CRUD. After trying to pass data to my view and into a table it said my variables are undefined, even if i tried each one separately.
ProductController.php
public function index(){
$products = Product::all();
return view('products.index', ['products' => $products]);
}
index.blade.php
<table border="1">
<tr>
<th>Id</th>
<th>Brand name</th>
<th>Product name</th>
<th>Quantity</th>
<th>Description</th>
<th>Price</th>
</tr>
@foreach($products as $product)
<tr>
<td>{{$product->$id}}</td>
<td>{{$product->$productBrand}}</td>
<td>{{$product->$productName}}</td>
<td>{{$product->$quantity}}</td>
<td>{{$product->$description}}</td>
<td>{{$product->$price}}</td>
</tr>
@endforeach
</table>
I have tried doing 'dd($products)' both in the view and inside the controller, which did work and show me the data. So to me it looks like it at least arrives at the view. Even if i do {{$product}} inside the foreach, i get data:
{"id":4,"productBrand":"Apple Brand","productName":"Apple","quantity":50,"description":"Red Apple","price":"1.00","created_at":"2023-06-21T00:09:31.000000Z","updated_at":"2023-06-21T00:09:31.000000Z"}
Please or to participate in this conversation.