“Posts Per Page”: How to Save Setting for Every User Individually

In typical project with list and pagination, you should have a choice to select, how many entries you want to see per page – 10, 25, 50, 100 etc. That setting probably refreshes the table or the whole page, but how to save it for the next time that user logs in, or visits the page in a week?

This is a screenshot from one of my project – a choice of “per page” number above the actual table.

When users click one of those entries, they are redirected to the URL: /posts?per_page=XXX, where XXX may be one of those options – 25, 50, or 100.

Pretty simple, right? But then if user goes away and comes back in a day, or a week, they probably would love that setting to stay for them, so they wouldn’t need to click that number again. So where to save it?

In PostController.php you probably have something like this:

$posts = Post::where('some_conditions', $some_variables)
  ->paginate($posts_per_page);

Now, the question is how to get that $posts_per_page so it would come not only from GET Request variable, but from your history?

There are two ways: first, you can save it in users DB table – add one field, and update the value there. But what if you don’t have any user system, and the list is public? Then you need to use Cookies.

So here’s the code to get that $posts_per_page from Cookies, or GET request.

$cookie_value = Cookie::get('posts_per_page');
$posts_per_page = $request->get('posts_per_page', $cookie_value);

$options = config('constants.posts_per_page_options');
if (!$posts_per_page || !in_array($posts_per_page, $options)) {
    $posts_per_page = config('constants.posts_per_page_default');
}

Cookie::queue('posts_per_page', $posts_per_page);

Let’s break it down.

First block:

$cookie_value = Cookie::get('posts_per_page');
$posts_per_page = $request->get('posts_per_page', $cookie_value);

It means – let’s try to get posts_per_page from GET request, if it’s not there, then let’s default to the data in the Cookie.

But what if there’s no Cookie? That’s the second block:

$options = config('constants.posts_per_page_options');
if (!$posts_per_page || !in_array($posts_per_page, $options)) {
    $posts_per_page = config('constants.posts_per_page_default');
}

We save possible pagination values, and a default value in config file – in my case it’s config/constants.php:

return [
    'posts_per_page_options' => [25, 50, 100],
    'posts_per_page_default' => 25,
];

In that case, we’re checking – if the value is not set in GET Request, or Cookie, or if it’s other than possible from config – then we default to the 25 per page.

Finally, last line is just saving that value into the Cookie again, for the future requests.

That’s it, hope it was helpful! If you don’t have much experience with pagination, here are a few more resources:

Like our articles?
Check out our Laravel online courses!

2 COMMENTS

    • I don’t it’s suitable in a Middleware, cause it may depend on which page/Controller you are using. Maybe a better place for it would be Service, like PaginationService with method $posts_per_page = (new PaginationService())->getPostsPerPage($request);

LEAVE A REPLY

Please enter your comment!
Please enter your name here