带有消息'... must return a relationship instance.'的逻辑异常



尝试在整个互联网上搜索这个错误,但都是徒劳的,所以作为最后的手段,我在StackOverflow上创建了一个问题。

我设置了两个简单的Eloquent模型:
1。教师(扩展Authenticable)-因为我在系统中使用MultiAuth
2.一般注意事项(扩展Eloquent/Model)

appTeacher.php

public function generalNotices()
{
$this->hasMany('AppModulesGeneralNotice');
}

appModulesGeneralNotice.php

public function teacher()
{
$this->belongsTo('AppTeacher');
}

这是我的一般通知迁移表:

database/migrations/***_create_general_notices_table.php

Schema::create('general_notices', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->longtext('description')->nullable();
$table->string('attachment')->nullable();
$table->integer('teacher_id')->unsigned();
$table->foreign('teacher_id')->references('id')->on('teachers');
$table->date('dated_on')->default(now());
$table->timestamps();
});

我还在数据库中植入了一个示例通知,以检查它是否有效。

当我试图访问这些关系中的任何一个时,我都会遇到错误。

$teacher = Teacher::find(1); $teacher->generalNotices;

LogicException,消息为"App\Teacher::generalNotices must return a relationship instance">

$notice = GeneralNotice::find(1); $notice->teacher;

LogicException,消息为"App\Modules\GeneralNotice::教师必须返回关系实例。">

如果我尝试访问成员函数,
$teacher->generalNotices()
$notice->teacher()

我得到

请帮忙。

您需要return关系,例如:

public function teacher()
{
return $this->belongsTo('AppTeacher');
}

最新更新