Laravel分页和模型计数返回重复的查询

发布于 2025-02-13 09:05:23 字数 861 浏览 1 评论 0原文

您好,有没有一种方法可以计数模型计数结果而不复制查询?

我正在获取所有用户(带有分页),并且在页面的顶部,我想计算用户模型并显示我总共有多少用户。 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2025-02-20 09:05:24

要获取返回的总项目:

$ 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文