Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带有消息';的逻辑异常;。。。必须返回关系实例;_Php_Laravel_Laravel 5_Eloquent - Fatal编程技术网

Php 带有消息';的逻辑异常;。。。必须返回关系实例;

Php 带有消息';的逻辑异常;。。。必须返回关系实例;,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,试图在整个互联网上搜索这个错误,但都是徒劳的,所以作为最后的手段,我在这里创建了一个关于StackOverflow的问题 我建立了两个简单而雄辩的模型: 1。教师(扩展可验证)-因为我正在为系统使用MultiAuth。 2。一般注意事项(扩展雄辩/模型) app\Teacher.php public function generalNotices() { $this->hasMany('App\Modules\GeneralNotice'); } public function

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

我建立了两个简单而雄辩的模型:
1。教师(扩展可验证)-因为我正在为系统使用MultiAuth。
2。一般注意事项(扩展雄辩/模型)

app\Teacher.php

public function generalNotices()
{
    $this->hasMany('App\Modules\GeneralNotice');
}
public function teacher()
{
    $this->belongsTo('App\Teacher');
}
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();
    });
app\Modules\GeneralNotice.php

public function generalNotices()
{
    $this->hasMany('App\Modules\GeneralNotice');
}
public function teacher()
{
    $this->belongsTo('App\Teacher');
}
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();
    });
这是我的一般通知迁移表:

database/migrations/***\u create\u general\u notices\u table.php

public function generalNotices()
{
    $this->hasMany('App\Modules\GeneralNotice');
}
public function teacher()
{
    $this->belongsTo('App\Teacher');
}
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->general通知;

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


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

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

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

$notice->teacher()

我得到null


请帮助。

您需要
返回关系,例如:

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

@AmruthPillai我只是做了和你完全一样的事情,哈哈,只有当我遇到这个答案时,我才突然意识到!添加注释,因为这是搜索结果中的第一个条目。与此问题不同,在某些情况下,您不想返回关系,问题在于如何调用该方法。在Laravel中,很容易习惯于
model->something
,但如果您没有定义关系,则将其作为一种方法调用,例如,
model->something()
(如果您没有创建关系?:)-当您在方法调用中忘记了
()
时,也会抛出此错误,因此laravel认为您正在调用关系调用,但您的方法并不正确:)例如
受保护的函数getAllowed(){…}
但随后
foreach($this->getAllowed as$some){…}
-注意缺少的