0

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> 

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Ajay
  • 13
  • 4

1 Answers1

0

Try the following.

Controller

public function index()
{
    $products = Product::orderBy('name', 'asc')->get();

    return view('products.index')->with('Products', $products);
}

View

<table id="example23" class="display nowrap table table-hover table-striped table-bordered">
    <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>
    @if(!$Products->isEmpty())
        @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
    @else
        <tr>
            <td>You do not have any products to display.</td>
        </tr>
    @endif
    </tbody>
</table>
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
  • In the Controller index() try... return view('products.index')->with('Products', $products); – Karl Hill Jan 07 '20 at 05:52
  • Also on the command line try: php artisan route:list. And post the result. – Karl Hill Jan 07 '20 at 05:53
  • Sorry !! error was appearing because of bootstrap model code below that table at view page for editing the data, as its trying to fetch from db without checking. Could you please help me out with updating the data with bootstrap modal i tried with version below 6, none of them were submitting. – Ajay Jan 07 '20 at 05:55
  • try to assign a value in like this `paginate(5)` – kealaxshan Jan 08 '20 at 11:17