shd's avatar
Level 1

Pagination from a user-selected form on a view

Hi,

I have a form on a view page that I would like to have control over the number of items that are returned by the paginator. What is the best way to achieve this?

For this example I have two controllers and related Eloquent models for Clients and News. Each client can only have their own news articles returned.

At the moment, I am storing the number of pagination items in the database, which is fine if we are setting each client's properties on the backend . However, what if we want to allow users control over how many articles they can view per page and let users return a large number of records, or filter the query based upon a set of drop-down parameters?

Current setup:

NewsController.php:

public function show($client, $news)
    {
        $articles = Client::find($client->id)->news()->paginate($client->pagination_items);

        return view('news.show', compact('news', 'client', 'articles'));
    }

news\show.blade.php:

@foreach ($articles as $article)
   <h2>{{ $article->title }}</h2>
   <p>{{ $article->body }}</p>
@endforeach

What i'd ideally like to incorporate on the view is something like the control below, which would allow users to select their own number of articles to display per page, without it being saved to the database:

{!! Form::select('pagination_items', array('20' => '20', '25' => '25', '50' => '50') !!}
0 likes
3 replies
Kryptonit3's avatar

you could add a query string to the url.

like mysite.com/form?limit=25 and perform checks in the controller for that input. $request->get('limit');

shd's avatar
Level 1

Thanks Kryptonit3,

I've used query strings in the past for this kind of operation, but I was just wondering if there was a more Laravel-y solution to the problem.

If not, I'll go with your suggestion.

Kryptonit3's avatar

If it is a widely used feature you could add a drop-down setting in their profile settings to either update the database column or cache it.

Please or to participate in this conversation.