gpt4 book ai didi

laravel - 对整数调用成员函数 addEagerConstraints()

转载 作者:行者123 更新时间:2023-12-02 00:19:51 25 4
gpt4 key购买 nike

我尝试立即加载关系:

$tournaments = Tournament::with('numCompetitors')->latest()->paginate(config('constants.PAGINATION'));

我在锦标赛中的关系返回一个整数:

public function numCompetitors()
{
return $this->competitors()->count(); // it returns 24
}

我得到:

Call to a member function addEagerConstraints() on integer

我不明白为什么它会失败。

最佳答案

你做错了。如果要统计关系,请使用 withCount()具有正确定义的关系:

Tournament::withCount('competitors')->latest()->paginate(config('constants.PAGINATION'));

If you want to count the number of results from a relationship without actually loading them you may use the withCount method, which will place a {relation}_count column on your resulting models.

https://laravel.com/docs/5.4/eloquent-relationships#counting-related-models

关于laravel - 对整数调用成员函数 addEagerConstraints(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43898167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com