0

"Undefined variable: Pro (View: E:\xampp\htdocs\mypro\resources\views\folder\product.blade.php)" I am using laravel 5.6 trying to get data from existing database.

Product Model

       <?php

        namespace App;

        use Illuminate\Database\Eloquent\Model;


        class Product extends Model
        {


            protected $table='products';
            protected $primaryKey = 'p_id';


            protected $fillable = ['p_title', 'p_element', 'p_description',          'p_duration', 'p_amount', 'p_startDate', 'p_endDate', 'p_old_price', 'p_new_price', 'p_keyWords', 'p_category', 'p_status', 'prefertime'];

        }

Product Controller

            namespace App\Http\Controllers;

            use Illuminate\Http\Request;

            class ProductController extends Controller
            {

                public function index()
                {
                   $pro=Product::all();
                    return view('Folder.product'), compact('pro'));
                }       
            }

Product.blade.php @extends('layouts.app')

    @section('content')

      <h1>Products</h1>

     <table style="width:100%">
      <tr>
        <th>ID</th>
        <th>Name</th>
      </tr>
      <tr>

       @foreach($Pro as $row)
        <td>{{$row['p_id']}}</td>
        <td>{{$row['p_title']}}</td>
       @endforeach
      </tr>
    </table> 


    @endsection
tSharif
  • 3
  • 2
  • 3
    Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Spoody Apr 06 '18 at 20:47
  • return view('Folder'), compact('pro')); –  Apr 06 '18 at 21:06

2 Answers2

0

The capital, you need to use $pro not $Pro.

@foreach($pro as $row)
  <td>{{$row['p_id']}}</td>
  <td>{{$row['p_title']}}</td>
@endforeach

I hope it works.

Marlon Adarme
  • 357
  • 4
  • 15
  • I have changed the things as you say but still it's not working. It's showing "Undefined variable: pro (View: E:\xampp\htdocs\mypro\resources\views\folder\product.blade.php)" As far I can understand "$pro=Product::all();" this portion is not response properly . Any suggestion about that ? – tSharif Apr 07 '18 at 10:13
  • And if you delete the foreach of the view, what do you get? The view? – Marlon Adarme Apr 07 '18 at 11:28
0

As identified pro is not capitalised so you should correct as follows. You should also check that the array is not empty before you execute the page otherwise you can find yourself with more errors;

Also note that I've moved your 's inside the foreach as this is needed to make the table display properly.

@section('content')

  <h1>Products</h1>

 <table style="width:100%">
  <tr>
    <th>ID</th>
    <th>Name</th>
  </tr>


   @if(! empty($pro))
     @foreach($pro as $row)
     <tr>
       <td>{{$row['p_id']}}</td>
       <td>{{$row['p_title']}}</td>
    </tr>
     @endforeach
  @else
     <tr>
       <td colspan="2">No records</td>
    </tr>
  @endif
</table> 


@endsection

Also, in your controller the view should read return view('Folder.product', compact('pro')); not return view('Folder.product'), compact('pro'));

Simon R
  • 3,732
  • 4
  • 31
  • 39