I am trying to make a CRUD page using a single view. When I have data in the DB its displaying my table without any errors. However, if the DB is empty, its displays the error as the following.
Undefined variable: Product
Please help. What am I doing wrong?
ProductsController.php
public function index()
{
$products = Product::orderBy('name', 'asc')->paginate();
return view('products')->with('Products', $products);
}
public function create()
{
return view('products.create');
}
web.php
Route::resource('/products', 'ProductsController');
products.blade.php
<table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>NO</th>
<th>SKU</th>
<th>HSN CODE</th>
<th>PRODUCT NAME</th>
<th>QUANTITY</th>
<th>PRICE</th>
</tr>
</thead>
<tbody>
@foreach($Products as $Product)
<tr>
<td>{{ $Product->id }}</td>
<td>{{ $Product->sku }}</td>
<td>{{ $Product->hsn }}</td>
<td>{{ $Product->name }}</td>
<td>{{ $Product->quantity }}</td>
<td>{{ $Product->price }}</td>
<td><button type="button" class="btn btn-success editbtn">Edit</button></td>
</tr>
@endforeach
</tbody>