«

Laravel Orm paginate 存在一个判断优化

时间:2025-6-4 15:58     作者:wanzi     分类: php


    /**
     * Paginate the given query.
     *
     * @param  int|null  $perPage
     * @param  array  $columns
     * @param  string  $pageName
     * @param  int|null  $page
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     *
     * @throws \InvalidArgumentException
     */
    public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)

核心点:

  1. 先按条件做统计
    
    [2025-06-04 15:56:51] local.DEBUG: Executed SQL [1.36ms]: select * from `v2_user` where `v2_user`.`id` = 2 limit 1

[2025-06-04 15:56:51] local.DEBUG: Executed SQL [0.29ms]: select count(*) as aggregate from control_plans where (name like %2222%)


2.  如果count == 0 就不会select

3. select

[2025-06-04 15:57:20] local.DEBUG: Executed SQL [1.99ms]: select * from v2_user where v2_user.id = 2 limit 1

[2025-06-04 15:57:20] local.DEBUG: Executed SQL [0.32ms]: select count(*) as aggregate from v2_plans where (name like %方案%)

[2025-06-04 15:57:20] local.DEBUG: Executed SQL [0.51ms]: select * from v2_plans where (name like %方案%) order by created_at desc limit 10 offset 0