Skip to content

Instantly share code, notes, and snippets.

@renalpha
Created August 6, 2019 13:05
Show Gist options
  • Save renalpha/7ebdc890b405a19240f04955e000270a to your computer and use it in GitHub Desktop.
Save renalpha/7ebdc890b405a19240f04955e000270a to your computer and use it in GitHub Desktop.
Laravel save pagination filters in session
<?php
class OverviewController
{
/**
* Process the request and apply to a session.
*
* @param \Illuminate\Http\Request $request
* @param string $filter
* @return \Illuminate\Http\Request
*/
protected function saveFiltersToSession(Request $request, string $filter): Request
{
$subjectRequest = $request->except(['page', 'search']);
if (count($subjectRequest) === 0) {
// Apply session filtering
if (!$request->session()->has($filter)) {
$request->session()->put($filter, $subjectRequest);
}
// New request params set, so set the new session
} else {
$request->session()->put($filter, $subjectRequest);
}
$request = $request->merge($request->session()->get($filter));
return $request;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment