我对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
的命名空间,但如果保持默认,则应为$this->belongsTo("App\Category", ...);
。 - Tim Lewis