Laravel Builder.php第2405行中的BadMethodCallException:调用未定义的方法Illumb\Database\Query\Builder::AddagerConstraints()
当我将Laravel Builder.php第2405行中的BadMethodCallException:调用未定义的方法Illumb\Database\Query\Builder::AddagerConstraints(),laravel,Laravel,当我将auth()->user()->roles()与tinker一起使用时,它可以正常工作,但当我在网页上使用它时,我得到: BadMethodCallException in Builder.php line 2405: Call to undefined method Illuminate\Database\Query\Builder::addEagerConstraints() 这意味着什么,这可能是什么原因?以下是相关模型: class User extends Authentica
auth()->user()->roles()
与tinker一起使用时,它可以正常工作,但当我在网页上使用它时,我得到:
BadMethodCallException in Builder.php line 2405:
Call to undefined method Illuminate\Database\Query\Builder::addEagerConstraints()
这意味着什么,这可能是什么原因?以下是相关模型:
class User extends Authenticatable
{
public function roles(){
return Role::where('id', 6);
}
}
我使用的是Laravel5.2,完全错误如下
附言:我想问题与我在网站其他部分使用它的方式有关。这就是为什么它对tinker有效。其他部分看起来需要关系obj形式的结果,我在where查询后无法提供该结果
这是正常关系$this->belongTomany(Role::class)的dd代码>
这是修改后的返回角色::where('id',6)代码>一
Builder {#812 ▼
#query: Builder {#317 ▶}
#model: Role {#997 ▶}
#eagerLoad: []
#macros: []
#onDelete: null
#passthru: array:11 [▶]
#scopes: []
#removedScopes: []
}
他们看起来很不一样,这可能就是原因
正如@SimonSvensson所问的,这里是我的评论
模型
class Comment extends Model
{
/**
* Fields that can be mass assigned.
*
* @var array
*/
protected $fillable = ['active', 'user_id', 'text', 'url'];
/**
* Comment morphs to models in commentable_type.
* Get all of the owning commentable models.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function commentable()
{
// morphTo($name = commentable, $type = commentable_type, $id = commentable_id)
// requires commentable_type and commentable_id fields on $this->table
return $this->morphTo();
}
/**
* Query scope ofWebsite.
*
* @param \Illuminate\Database\Eloquent\Builder
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeOfWebsite($query)
{
return $query->where(function($q){
$q->whereNull('commentable_id')->whereNull('commentable_type');
});
}
/**
* Comment belongs to User.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
// belongsTo(RelatedModel, foreignKey = user_id, keyOnRelatedModel = id)
return $this->belongsTo(User::class);
}
}
看起来您正试图加载User.roles关系,但这不是一个正确的关系,它只是一个调用Role::where(…)的方法。关系方法通常返回类似于$this->hasMany(Role::class)
的内容。您能提供您正在使用的Laravel的确切版本以及错误的完整堆栈跟踪吗?当然@SimonSvensson,我将添加(编辑)它。你能给我们看一下评论模型吗?@SimonSvensson是的,我会添加它。是的,你说得对,我会从这个角度重新考虑它。
Builder {#812 ▼
#query: Builder {#317 ▶}
#model: Role {#997 ▶}
#eagerLoad: []
#macros: []
#onDelete: null
#passthru: array:11 [▶]
#scopes: []
#removedScopes: []
}
class Comment extends Model
{
/**
* Fields that can be mass assigned.
*
* @var array
*/
protected $fillable = ['active', 'user_id', 'text', 'url'];
/**
* Comment morphs to models in commentable_type.
* Get all of the owning commentable models.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function commentable()
{
// morphTo($name = commentable, $type = commentable_type, $id = commentable_id)
// requires commentable_type and commentable_id fields on $this->table
return $this->morphTo();
}
/**
* Query scope ofWebsite.
*
* @param \Illuminate\Database\Eloquent\Builder
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeOfWebsite($query)
{
return $query->where(function($q){
$q->whereNull('commentable_id')->whereNull('commentable_type');
});
}
/**
* Comment belongs to User.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
// belongsTo(RelatedModel, foreignKey = user_id, keyOnRelatedModel = id)
return $this->belongsTo(User::class);
}
}