searchable: SQL error when paginate after search on Laravel v5.1.9+

Hi,

I got this error when I perform paginate on Laravel version 5.1.9 and above:

QueryException in Connection.php line 636:
SQLSTATE[HY000]: General error: 2031 (SQL: select count(*) as aggregate from (select ....

My code:

user::search('John', null, true)->paginate(10);

This error does not occur on normal get()

user::search('John', null, true)->get() // no error;

It also does not occur on Laravel v5.1.8 and below

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 4
  • Comments: 62 (9 by maintainers)

Most upvoted comments

Another temporary solution similar to @woodytang, but as a single helper function instead (needs to be included globally in a helpers file of some sorts):

/**
 * Paginate the given collection.
 *
 * @param \Illuminate\Support\Collection $collection
 * @param int                            $perPage
 * @param string                         $pageName
 * @param int|null                       $page
 *
 * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
 */
function paginateCollection($collection, $perPage = 15, $pageName = 'page', $page = null)
{
    $page = $page ?: \Illuminate\Pagination\Paginator::resolveCurrentPage($pageName);
    $page = (int) max(1, $page); // Handle pageResolver returning null and negative values
    $path = \Illuminate\Pagination\Paginator::resolveCurrentPath();

    return new \Illuminate\Pagination\LengthAwarePaginator(
        $collection->forPage($page, $perPage),
        count($collection),
        $perPage,
        $page,
        compact('path', 'pageName')
    );
}

Usage:

$users = App\User::search($searchQuery)->get();
$users = paginateCollection($users, 10);

Note that it will retrieve ALL results, and trim the resulting collection.

Yup tested in Laravel 5.2, works like a charm

#126 should solve the problem

Guys, why don’t you abandon this package? This bug has been open for 12 months, with no movement. I was great while it lasted, but Laravel 5.3 introduced Scout, and with Scout you can easily use Algolia (and their free tier).

I did the switch the other day, took me less than 30 minutes, and has a bunch more other benefits.

We need to accept the fact this guy don’t care this bug, someone knows another packages like this?