laravel 解决paginate查询多个字段报错的问题

吾爱主题 阅读:124 2021-09-13 16:33:00 评论:0

用laravel 框架想实现一个分页接口,返回数据表中其中一部分字段

?
1 2 3 $list = DB::table( 'booklist_table' )->orderBy( 'create_time' , 'asc' )->paginate( $size ,[ 'id' , 'title' , 'author' , 'image' ])->toJson();   return json_decode( $list ,true);

结果运行报错

?
1 "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' `title`, `author`, `image`) as aggregate from `booklist_table`' at line 1 (SQL: select count(`id`, `title`, `author`, `image`) as aggregate from `booklist_table`) ◀"

后来经过查询原因将代码改为

?
1 $list = DB::table( 'booklist_table' )->select( 'id' , 'title' , 'author' , 'image' )->orderBy( 'create_time' , 'asc' )->paginate( $size )->toJson();

结果正确!

以上这篇laravel 解决paginate查询多个字段报错的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/liudongForever/article/details/79973964

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容