当我使用auth()->user()->roles()
修补它时,它没有任何问题,但当我在网页上使用它时,我得到:
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);
}
}
我正在使用laravel 5.2,完整的错误如下所示。
PS:我想这个问题与我在网站其他部分使用它的方式有关。这就是为什么它适用于修补。其他部分看起来需要 Relation obj 形式的结果,我无法在where查询后提供。
BadMethodCallException in Macroable.php line 74:
Method addEagerConstraints does not exist.
in Macroable.php line 74
at Collection->__call('addEagerConstraints', array(array(object(User), object(User)))) in Builder.php line 667
at Collection->addEagerConstraints(array(object(User), object(User))) in Builder.php line 667
at Builder->loadRelation(array(object(User), object(User)), 'roles', object(Closure)) in Builder.php line 645
at Builder->eagerLoadRelations(array(object(User), object(User))) in Builder.php line 322
at Builder->get()
at call_user_func_array(array(object(Builder), 'get'), array()) in Relation.php line 343
at Relation->__call('get', array()) in Relation.php line 113
at BelongsTo->get() in Relation.php line 113
at Relation->getEager() in Builder.php line 676
at Builder->loadRelation(array(object(Comment), object(Comment), object(Comment)), 'user', object(Closure)) in Builder.php line 645
at Builder->eagerLoadRelations(array(object(Comment), object(Comment), object(Comment))) in Builder.php line 322
at Builder->get() in ModuleController.php line 27
at ModuleController->main(array())
at call_user_func_array(array(object(ModuleController), 'main'), array(array())) in Container.php line 507
at Container->call(array(object(ModuleController), 'main'), array(array())) in Container.php line 602
at Container->callClass('\App\Modules\Preview_coupons_comments\Controllers\ModuleController@main', array(array()), null) in Container.php line 502
at Container->call('\App\Modules\Preview_coupons_comments\Controllers\ModuleController@main', array(array())) in Facade.php line 219
at Facade::__callStatic('call', array('\App\Modules\Preview_coupons_comments\Controllers\ModuleController@main', array(array()))) in PageController.php line 220
at App::call('\App\Modules\Preview_coupons_comments\Controllers\ModuleController@main', array(array())) in PageController.php line 220
at PageController->getPageByName()
at call_user_func_array(array(object(PageController), 'getPageByName'), array()) in Controller.php line 80
at Controller->callAction('getPageByName', array()) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(PageController), object(Route), 'getPageByName') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(PageController), object(Route), object(Request), 'getPageByName') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\PageController', 'getPageByName') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckActive.php line 23
at CheckActive->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckActive), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Debugbar.php line 51
at Debugbar->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Debugbar), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
at require_once('/Users/skeletor/Sites/project/public/index.php') in server.php line 21
这是正常关系$this->belongsToMany(Role::class);
BelongsToMany {#971 ▼
#table: "role_user"
#foreignKey: "user_id"
#otherKey: "role_id"
#relationName: "roles"
#pivotColumns: []
#pivotWheres: []
#pivotWhereIns: []
#pivotCreatedAt: null
#pivotUpdatedAt: null
#query: Builder {#996 ▶}
#parent: User {#319 ▶}
#related: Role {#812 ▶}
}
这是修改后的return Role::where('id', 6);
一个
Builder {#812 ▼
#query: Builder {#317 ▶}
#model: Role {#997 ▶}
#eagerLoad: []
#macros: []
#onDelete: null
#passthru: array:11 [▶]
#scopes: []
#removedScopes: []
}
他们看起来很不一样,这可能就是原因。
@SimonSvensson在这里问的是我的Comment
模型
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);
}
}
答案 0 :(得分:1)
看起来你正试图加载User.roles关系,但它不是一个正确的关系,它只是一个调用Role :: where(...)的方法。关系方法通常返回$this->hasMany(Role::class)
。