laravel 带有消息“...必须返回关系实例”的 LogicException。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46843914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:50:20  来源:igfitidea点击:

LogicException with message '... must return a relationship instance.'

phplaravellaravel-5eloquent

提问by Amruth Pillai

Tried searching the entire internet for this error, but all in vain, so as a last resort, I'm creating a question here on StackOverflow.

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

I have two simple Eloquent Models set up:
1. Teacher(extends Authenticable) - since I'm using MultiAuth for the system.
2. GeneralNotice(extends Eloquent/Model)

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

app\Teacher.php

app\Teacher.php

public function generalNotices()
{
    $this->hasMany('App\Modules\GeneralNotice');
}

app\Modules\GeneralNotice.php

app\Modules\GeneralNotice.php

public function teacher()
{
    $this->belongsTo('App\Teacher');
}

This is my migration table for General Notices:

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

database/migrations/***_create_general_notices_table.php

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();
    });

I have also seeded the database with a sample notice, to check if it works.

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

When I try to access any of these relations, I run into an error.

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

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

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

LogicException with message 'App\Teacher::generalNotices must return a relationship instance.'

带有消息“App\Teacher::generalNotices 必须返回关系实例”的 LogicException。

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

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

LogicException with message 'App\Modules\GeneralNotice::teacher must return a relationship instance.'

LogicException 消息为“App\Modules\GeneralNotice::teacher must return a relationship instance”。

If I try to access the member function,
$teacher->generalNotices()OR
$notice->teacher()

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

I get null.

我得到null

Please help.

请帮忙。

回答by Alexey Mezenin

You need to returnrelationship, for example:

你需要建立return关系,例如:

public function teacher()
{
    return $this->belongsTo('App\Teacher');
}