Eloquent致命错误:在第451行的\ illuminate \ database \ Eloquent \ Builder.php中,调用未定义方法Eloquent \ Collection :: addEagerConstraints() [英] Eloquent Fatal error: Call to undefined method Eloquent\Collection::addEagerConstraints() in \illuminate\database\Eloquent\Builder.php on line 451

查看:196
本文介绍了Eloquent致命错误:在第451行的\ illuminate \ database \ Eloquent \ Builder.php中,调用未定义方法Eloquent \ Collection :: addEagerConstraints()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循教程将关系保存在一个表中,以确定一个用户是否是另一个用户的朋友,反之亦然.

I am following a tutorial where you can save relationships in one table to determine if a user is friend of another user and viceversa.

就我而言,我需要将那些绑定在一起的项目保存在关系表boundTogether_projects中.

In my case, I need to save in a relational table boundTogether_projects those projects that are bound together.

因此在Project.php模型中,我创建了以下口才关系:

So inside the Project.php Model I created the following Eloquent relationships:

<?php
    /*** Bound together projects ***/
    public function projectsBoundToThisOne(){
        return $this->belongsToMany('Models\User\Project', 'boundTogether_projects', 'bound_id', 'project_id');
    }
    public function boundTo(){
        return $this->belongsToMany('Models\User\Project', 'boundTogether_projects', 'project_id', 'bound_id');
    }
    public function boundTogetherProjects(){
        return $this->projectsBoundToThisOne()->wherePivot('bound',true)->get()->merge($this->boundTo()->wherePivot('bound', true)->get());
    }

这不起作用,因为出现以下错误:

This is not working, since I get the following error:

致命错误:调用未定义的方法 在以下位置照亮\ Database \ Eloquent \ Collection :: addEagerConstraints() 451行上的\ illuminate \ database \ Eloquent \ Builder.php

Fatal error: Call to undefined method Illuminate\Database\Eloquent\Collection::addEagerConstraints() in \illuminate\database\Eloquent\Builder.php on line 451

当我将此方法称为紧急加载时,问题似乎就解决了.也就是说:

Looks like the problem comes right when I call this method as eager loading. That is to say:

$projects = Project::with('boundTogetherProjects');

雄辩的版本是5.1

我想念什么?我该如何解决?

What am I missing? How do I fix it?

推荐答案

这可能是因为boundTogetherProjects不是关系方法.它很容易从关系中合并一些数据.因此有效的方法是:

This is probably because boundTogetherProjects is not relationship method. It simple gets some data from relationship merged to the other. So the valid way would be:

$projects = Project::with('projectsBoundToThisOne','boundTo');

,然后对于每个项目,您可以运行boundTogetherProjects来获取项目

and then for each project you could run boundTogetherProjects to get the projects

$projects->each(function($project) {
  dd($project->boundTogetherProjects());
});

这篇关于Eloquent致命错误:在第451行的\ illuminate \ database \ Eloquent \ Builder.php中,调用未定义方法Eloquent \ Collection :: addEagerConstraints()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆