Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

calmiraged88's avatar

Undefined variable: cat.. and Undefined property: Illuminate\Database\Eloquent\Collection::$id

I currently starts learning Laravel 5, a hard journey.. (bitter smile)

Cat model :

namespace Furbook;
use Illuminate\Database\Eloquent\Model;

class Cat extends Model {
  protected $table = 'cats';
  protected $fillable = ['name','date_of_birth','breed_id'];
  public function breed() {
    return $this->belongsTo('Breed');
  }
}

Breed model :

namespace Furbook;
use Illuminate\Database\Eloquent\Model;

class Breed extends Model {
  protected $table = 'breeds';
  public $timestamps = false;
  public function cats() {
    return $this->hasMany('Cat');
  }
}

My Route :

Route::get('cats', 'CatController@index');

My CatController Controller :

namespace Furbook\Http\Controllers;
use Furbook\Cat;

class CatController extends Controller {
    public function index()
    {
        $cats = Cat::all();
        return view('cats.index')->with('cats', $cats);
    }

My Index (some-of) :

  @forearch ($cats as $cat)
    <div class="cat">
      <a href="{{ url('cats/'.$cat->id) }}">
        <strong>{{ $cat->name }}</strong> - {{ $cat->breed->name }}
      </a>
    </div>
  @endforearch

error I got : ErrorException in 813bbac2993252b4fe446fe2cb1aad76 line 21: Undefined variable: cat (View: C:\xampp\htdocs\laravelcats\resources\views\cats\index.blade.php)

For more references, then I edit the forearch to this :

  @forearch ($cats)
    <div class="cat">
      <a href="{{ url('cats/'.$cats->id) }}">
        <strong>{{ $cats->name }}</strong> - {{ $cats->breed->name }}
      </a>
    </div>
  @endforearch

and got another error : ErrorException in 813bbac2993252b4fe446fe2cb1aad76 line 21: Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: C:\xampp\htdocs\laravelcats\resources\views\cats\index.blade.php)

Then I give up, I create laracasts account and make this topic. So... Hi Laravel users~ Teach me how to fix this please X)

0 likes
4 replies
Northofoleander's avatar

Forget about that last part.. which is not the correct way to use a foreach.

So to your first error. it does not seems like there is an error in any of the code you have pasted in here. I am suspecting you are using that variable outside the foreach scope, so could you maybe check for that, or just simply paste a gist in here(of you blade file), so we can check it for you.

1 like
veve286's avatar

// change your a tag like this


@forearch ($cats as $cat)
    <div class="cat">
      <a href='{{ url("cats/$cat->id") }}'>
        <strong>{{ $cat->name }}</strong> - {{ $cat->breed->name }}
      </a>
    </div>
  @endforearch
1 like
calmiraged88's avatar

First of all, thanks for see and replying my thread! (nor trying to solve my problems!)

@veve286 : What a nice way to do href tag, I'll use it in the future. @Northofoleander : thanks for introducing gist to me, I'm still new in this developing world anyway @zachleigh : In my reference book it's using forearch, so it's a typo then :D thanks for the solution

I used all of your references, guys! Thank you very much ^^

Please or to participate in this conversation.