php – laravel paginate在使用select case和parameter binding时
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – laravel paginate在使用select case和parameter binding时,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1588字,纯文字阅读大概需要3分钟。
内容图文
![php – laravel paginate在使用select case和parameter binding时](/upload/InfoBanner/zyjiaocheng/768/c5cb98c6de044ff09f0495704c745093.jpg)
Laravel版本:5.5
PHP版本:7
嗨,我想执行这个查询:
select (case
when(title like 'my-keyword') then 1
when(description like 'my-keyword') then 2
) as ordering from products where id > 10;
当我通过查询生成器执行此操作时:
$products = DB::table('products')->select(DB::raw('(case
when(title like '?') then 1
when(description like '?') then 2
) as ordering'))->where('id', '>', 10)->setBinding(['my-keyword', 'my-keyword'])->paginage(10);
这将得到计数,因为我们知道这将删除所有选择部分并将其替换为count(*)作为聚合,所以如果我在此查询构建器上使用setBindings并传递[‘my-keyword’,’my-keyword’]对此聚合的查询将更改为:
select count(*) as aggregate from products where id > my-keyword;
因此,这将导致在此查询和其他替代方法(例如此查询)上使用分页的问题!
为了解决这个问题,我更改了/…./Query/Builder.php中的一些代码:
$total = $this->getCountForPagination($columns);
对此:
$all = $this->get();
$total = $all->count();
对于这种情况,我知道这是错的,但现在它的确有效!
我该怎么做以正确的方式解决这个问题?!
解决方法:
你可以试试这个:
写下这样的原始查询:
DB::select('RAW_QUERY');
It would return an array. You can use LengthAwarePaginator to paginate the array like so:
use Illuminate\Pagination\LengthAwarePaginator;
$this->paginateArray($array, $perPage);
public function paginateArray($items, $perPage)
{
$pageStart = \Request::get('page', 1);
$offSet = ($pageStart * $perPage) - $perPage;
$itemsForCurrentPage = array_slice($items, $offSet, $perPage, true);
return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage, Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
}
内容总结
以上是互联网集市为您收集整理的php – laravel paginate在使用select case和parameter binding时全部内容,希望文章能够帮你解决php – laravel paginate在使用select case和parameter binding时所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。