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)