简体   繁体   English

Laravel雄辩的查询限制和偏移量被自动添加以计算查询

[英]Laravel Eloquent query limit and offset is being added automatically to count query

Limit and offset is being added automatically to my following query although I am not adding any limit and offset into it. 虽然我没有在其中添加任何限制和偏移量,但是限制和偏移量会自动添加到以下查询中。

$total_trashed_records = $query_records_trashed
                             ->onlyTrashed()
                             ->count();

Below are are my queries. 以下是我的查询。 Last query is just to return total number of users in trash but limit and offset are being added and it isn't working. 最后一个查询只是返回垃圾桶中的用户总数,但是正在添加limitoffset ,因此它不起作用。

Below are my queries. 以下是我的查询。

$fields = ["id", "first_name","last_name","username","user_type","job_title","status"];

  $query_records = User::select($fields)
                           ->Where('first_name', 'like', '%' .$s. '%');

   $query_records_trashed = $query_records;    
   $total_records = $query_records->count();

   $records = $query_records
                  ->orderBy($sort_column, $sort_order)
                  ->skip($page*$rows_per_page)
                  >take($rows_per_page)
                  ->get();

  $total_trashed_records = $query_records_trashed
                               ->onlyTrashed()
                               ->count();

Below is the result of last query in query log. 以下是查询日志中最后一次查询的结果。

"query" => "select count(*) as aggregate from `users` where `first_name` like ?  and `users`.`deleted_at` is not null limit 50 offset 0",
"bindings" => array:1 [
      0 => "%%"
     ]

"time" => 0.67

To remove the limit and offset from the produced query, you'd need to remove these two lines from your query builder: 要从产生的查询中删除limitoffset ,您需要从查询生成器中删除这两行:

->skip($page*$rows_per_page)
->take($rows_per_page)

which equate to offset and limit respectively. 分别等于offsetlimit


Check this section of the documentation. 检查文档的此部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM