【发布时间】:2015-10-28 23:26:38
【问题描述】:
我对 Laravel 很陌生。
我尝试在我的代码中使用一个示例,但结果出现错误 - FatalErrorException in Model.php line 827: Class Category not found。
之后我修改了一行代码并修复了一个错误。但是,我实际上不明白错误的原因以及如何修复它。
这是我的代码(因此,当我尝试使用 category 构建查询时 - 我收到错误):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Category;
class Translation extends Model
{
protected $fillable = array('lang1', 'lang2', 'lang1_code', 'lang2_code', 'category_id', 'created_at', 'updated_at');
public function category() {
return $this->belongsTo('Category', 'category_id');
//return $this->belongsTo('App\Category', 'category_id');
}
}
但是,当我修改一行时,我没有得到上述错误:
旧行(错误):return $this->belongsTo('Category', 'category_id');
新行(无错误):return $this->belongsTo('App\Category', 'category_id');
【问题讨论】:
-
Category和Translation文件类的路径是什么? -
查看
Category的命名空间,但如果保留默认,应该是$this->belongsTo("App\Category", ...);