【问题标题】:laravel paginator is not working with LIKE in where clauselaravel 分页器在 where 子句中不使用 LIKE
【发布时间】:2018-10-10 19:37:23
【问题描述】:

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 值

【问题讨论】:

  • 实际的搜索词来自哪里?是网址参数吗?
  • yes from url parameter, sent from a search input texbox,
  • 静态文本不会出现同样的问题,几个小时我都头疼,tks!!!

标签: laravel paginator


【解决方案1】:

你必须append这样的自定义值到分页链接:

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

【讨论】:

    猜你喜欢
    • 2013-06-30
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 2019-09-08
    相关资源
    最近更新 更多