如何解决laravel中多字段paginate查询时出现的错误问题?

更新于
2026-09-28 18:15:39
10阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计243个文字,预计阅读时间需要1分钟。

使用Laravel框架实现一个分页接口,返回数据表中特定字段的JSON格式数据:phppublic function paginateBooks($size) { $list=DB::table('booklist_table') ->orderBy('create_time', 'asc') ->paginate($size, ['id', 'title', 'author', 'image']) ->toJson(); return json_decode($list, true);}

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

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

结果运行报错

"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`) ◀"

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

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

结果正确!

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

本文共计243个文字,预计阅读时间需要1分钟。

使用Laravel框架实现一个分页接口,返回数据表中特定字段的JSON格式数据:phppublic function paginateBooks($size) { $list=DB::table('booklist_table') ->orderBy('create_time', 'asc') ->paginate($size, ['id', 'title', 'author', 'image']) ->toJson(); return json_decode($list, true);}

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

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

结果运行报错

"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`) ◀"

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

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

结果正确!

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