Dani
Dani

Reputation: 29

Undefined variables in index.blade view

I'm learning Laravel and I have an stupid error when I try to show a list in a view. I have a table called "catalogs" with some entries, and I can't show this entries in a view.

I've been looking for the problem for hours and I can not solve it... I've done this before, but now I can't find the problem...

This is my code

CatalogController (index function)

 namespace TBZPlus\Http\Controllers;

 use TBZPlus\Catalog;
 use Illuminate\Http\Request;

 class CatalogController extends Controller
 {
     /**
      * Display a listing of the resource.
      *
      * @return \Illuminate\Http\Response
      */

      public function index()
     {

         $catalogs = Catalog::all();

         return view('catalogs.index',compact('catalogs'));
     }

Model Catalog.php

 <?php

 namespace TBZPlus;

 use Illuminate\Database\Eloquent\Model;

 class Catalog extends Model
 {
     //
 }

Route

 Route::resource('catalogs', 'CatalogController');

UP function (mgiration)

public function up()
{
    Schema::create('catalogs', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('xmlcatalogo');
        $table->timestamps();
    });
}

VIEW (Only the foreach)

 @foreach($catalogs as $catalog)
 <tr>
 <td>{{$catalog->$id}}</td>
 <td>{{$catalog->$name}}</td>
 <td>{{$catalog->$xmlcatalogo}}</td>
 </tr>
 @endforeach

Upvotes: 1

Views: 63

Answers (2)

Abhishek Trivedee
Abhishek Trivedee

Reputation: 11

1.please specify your table in model

=>Catalog.php

  <?php

namespace TBZPlus;

use Illuminate\Database\Eloquent\Model;

class Catalog extends Model
{
   protected $table = 'your table name';
}

2.change in view file

@foreach($catalogs as $catalog)
<tr>
   <td>{{$catalog->id}}</td>
   <td>{{$catalog->name}}</td>
   <td>{{$catalog->xmlcatalogo}}</td>
</tr>
@endforeach

Upvotes: 1

nakov
nakov

Reputation: 14278

Can you try using this instead:

 @foreach($catalogs as $catalog)
 <tr>
 <td>{{$catalog->id}}</td>
 <td>{{$catalog->name}}</td>
 <td>{{$catalog->xmlcatalogo}}</td>
 </tr>
 @endforeach

Note the $ before the field name is gone. Because you use a property on the $catalog instance.

Upvotes: 3

Related Questions