Laravel分页和模型计数返回重复的查询
您好,有没有一种方法可以计数模型计数结果而不复制查询?
我正在获取所有用户(带有分页),并且在页面的顶部,我想计算用户模型并显示我总共有多少用户。 debugbar返回,我已经重复了计数(*)查询,但没有任何问题,但是我想知道是否有一种方法可以在不复制查询的情况下获得两个结果。
查询的IMG:
这些是引起函数问题“:
public static function getUsers() {
return User::withoutTrashed()->select(['id', 'name', 'email', 'status', 'isAdmin', 'isMember'])->paginate(15);
}
public static function getUsersCount() {
return User::count();
}
,这是视图的提供的数据:
$users = User::getUsers();
$usersCount = User::getUsersCount();
return view('dashboard.users.index', [
'users' => $users,
'usersCount' => $usersCount
]);
Hello is there a way to count model count results without duplicating the query?
I am getting all users (with pagination) and at the top of the page, I want to count the User model and display how many users I have in total. The debugbar returns that I have duplicated the count(*) query and it does not make any problems but I'd like to know if there is a way to get both results without duplicating the query.
IMG OF QUERIES:
These are the functions that are "causing the issue":
public static function getUsers() {
return User::withoutTrashed()->select(['id', 'name', 'email', 'status', 'isAdmin', 'isMember'])->paginate(15);
}
public static function getUsersCount() {
return User::count();
}
And here is the provided data for the view:
$users = User::getUsers();
$usersCount = User::getUsersCount();
return view('dashboard.users.index', [
'users' => $users,
'usersCount' => $usersCount
]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要获取返回的总项目:
$ Paginator-> total()
这需要是一个lengeawarepaginator: https://laravel.com/docs/9.x/pagination#paginator-instance-methods
To get the total items returned call:
$paginator->total()
This needs to be one a LengthAwarePaginator: https://laravel.com/docs/9.x/pagination#paginator-instance-methods