/About /Blog /Reviews /Resources

mathieu@laptop:~/Content/Laravel$ cat invalid-parameter-number-in-laravel-cursor-pagination.txt
> Title: Invalid parameter number in Laravel Cursor Pagination
> Date: January 19, 2025
> Tags: Laravel

I was working with raw queries and selects in Laravel in combination with Laravel’s Cursor Pagination. This worked fine until I want to the next page.

If you encounter the following error in combination with selectRaw() or whereRaw() in Laravel, I hope this solution works for you.

SQLSTATE[HY093]: Invalid parameter number (Connection: mysql, SQL: ...)

The reason for this is that cursor pagination is not using offset, but rather order by ?. In my case, I was ordering the results by a field _score. Yours might be different.

$query = Model::query()
    ->selectRaw('MATCH(`subject`, `body`) AGAINST(? IN BOOLEAN MODE) as _score', ["'". $search ."'"])
    ->whereRaw('MATCH(`subject`, `body`) AGAINST(? IN BOOLEAN MODE) > 0', ["'". $search ."'"]);

if (request()->get('cursor')) {
    $cursorPaginator = CursorPaginator::resolveCurrentCursor('cursor', null);
    $query->addBinding($cursorPaginator->parameter('_score'), 'where'); // Make sure parameter _score is available
}

$result = $query->cursorPaginate(20);

dump($result);

Good luck!

Composer update hanging on Loading composer repositories with package information
Dynamically change the mesh component of niagara effect