首页 > 解决方案 > laravel 分页器在 where 子句中不使用 LIKE

问题描述

laravel 分页器在 where 子句中不使用 LIKE

laravel/框架版本:v5.6.33

控制器

$search_qs = $request->input('search');

$query = Article::where("status", 2);
$query->where('title', 'LIKE', '%' . $search_qs . '%');
$articles = $query->paginate(3);

看法

{{ $articles->links() }}

数据库查询

select * from `articles` where `status` = '2' and `title` LIKE '%txt2search%' limit 3 offset 0

!!!好的 !!!

但是,当我点击分页器中的第 2 页时

select * from `articles` where `status` = '2' and `title` LIKE '%%' limit 3 offset 3

绑定

0. 2            (`status` = ?)

1. %%           (`title` LIKE ?)

值应该存储在 session 或 flash_session 中?但未检索到 LIKE 值

标签: laravelpaginator

解决方案


您必须将这样的自定义值附加到分页链接:

$articles = $query->paginate(3)->appends($request->only('search_qs'));

推荐阅读