Search code examples
laravelpaginator

laravel paginator is not working with LIKE in where clause


laravel paginator is not working with LIKE in where clause

laravel/framework version: v5.6.33

controller

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

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

view

{{ $articles->links() }}

database queries

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

!!! OK !!!

but, when I click on page 2 in the paginator

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

Bindings

0. 2            (`status` = ?)

1. %%           (`title` LIKE ?)

values should be stored in session, or flash_session ?? but LIKE value is not retrieved


Solution

  • You have to append custom values like this to the pagination links:

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