0

How to display specified number of records on web page?

Like : Items per page: 25 | 50 | 75 |100 If I select 25 then 25 records should be displayed.. I want this in laravel 5.5.. can anyone help me?

5
  • Just pass the number of items per page to the paginate method, for example ->paginate(75). The Pagination Docs are pretty clear about that. Commented Jul 29, 2018 at 17:03
  • But I want this,->( Items per page: 25 | 50 | 75 |100) view in my index.blade.php and in my index function of page controller Commented Jul 29, 2018 at 17:09
  • 1
    Send the number via the request query string. For example: yourapp.com/users?perPage=100 and in your controller action just fetch that from the request Users::paginate(request()->get('perPage', 25);. This will get the 100 value from the request and if no perPage paramater was passed, it will default to 25. You also might want to inject the request into your controller action method (not use the request() helper) and in order to do that you might want to read the HTTP Requests Docs. Commented Jul 29, 2018 at 17:11
  • But what should I do in index.blade.php?? Should I use any if else? Commented Jul 29, 2018 at 17:15
  • 1
    You're offering very little context of what your current code looks like, so you might want to edit your question to include your current code so we can offer more accurate advice. Commented Jul 29, 2018 at 17:16

1 Answer 1

2

There is no default functionality for per page in Laravel till 5.7. So, you need to add some custom functionality.

Use get parameter where you paginate your record like this:

public function get_users(Request $request) {
    return view('users', Users::paginate($request->get('per_page', 25)));
}

And send per_page parameter from view.

There are several ways to send per_page parameter from view.

I am using LaravelCollective 5.4 for this purpose https://laravelcollective.com/docs/5.4/html#drop-down-lists:

{!! Form::open([ 'url' => route('users'), 'method' => 'get' ]) !!}
    {!! Form::select( 'per_page', [ '25' => '25', '50' => '50', '75' => '75', '100' => '100'], '25', array('onchange' => "submit()") ) !!}
{!! Form::close() !!}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.