How to Add Query Parameters to Laravel Pagination with Vue 3 and Inertia.js
In this lesson, we will see how to add query parameters to Laravel pagination with Vue 3 and inertia.js.
Our goal in this lesson is to preserve the parameters like ?search=bernard&page=2 during navigation.
Use the withQueryString method
To solve this problem, use the withQueryString method to ensure that Laravel keeps the query parameters in the pagination links.
/**
* Search for contacts by name
*/
public function findContactsByName(Request $request)
{
$contacts = auth()->user()->contacts()
->where('name','LIKE','%'.$request->query('searchTerm').'%')
->paginate(10)->withQueryString();
return Inertia::render('Home/Index',[
'contacts' => $contacts,
]);
}