我在laravel 5中创建了两个模型,Cat和Breed
...猫
<?php namespace App\Models;
use App\Models\Breed;
use Illuminate\Database\Eloquent\Model;
class Cat extends Model {
protected $fillable = array('name','date_of_birth','breed_id');
public function breed(){
return $this->belongsTo('Breed');
}
}
?>
...培育
<?php namespace App\Models;
use App\Models\Cat;
use Illuminate\Database\Eloquent\Model;
class Breed extends Model {
public $timestamps = false;
public function cats(){
return $this->hasMany('Cat');
}
}
?>
然后我将表格(猫和品种)和种子猫表迁移了一些数据,表格填充没有任何错误(从phpmyadmin查看)
然后在routes.php中,我删除了这段代码,
Route::get('cats/breeds/{name}', function($name){
$breed = Breed::whereName($name)->with('cats')->first();
return View::make('cats.index')
->with('breed', $breed)
->with('cats', $breed->cats);
});
当我加载页面test.com/cats/breeds/persian这个错误弹出...(test.com是我的虚拟主机,波斯语是插入的数据)
Model.php第895行中的FatalErrorException:未找到类'Cat'
线891-900,
public function hasMany($related, $foreignKey = null, $localKey = null)
{
$foreignKey = $foreignKey ?: $this->getForeignKey();
$instance = new $related;
$localKey = $localKey ?: $this->getKeyName();
return new HasMany($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
}
我包括,
use App\Models\Cat;
在Model.php文件中,但此错误仍然存在,请帮助
答案 0 :(得分:0)
替换
Sql Server Compact Edition
与
return $this->belongsTo('Breed');
和
return $this->belongsTo('App\Models\Breed');
与
return $this->hasMany('Cat');
还从您的Model.php中删除return $this->hasMany('App\Models\Cat');