Lars-Janssen's avatar

Laravel pagination with dropdown

Hello,

I've got a route that returns Software::paginate(15);.

So what is the best practice if I want to show all software items within a dropdown menu? Should I make a separate route that returns Software::alll(); or are there better ways of handling this?

Thanks!

0 likes
6 replies
SaeedPrez's avatar

@lars64 I'm not sure I understand what you want to do.. do you want to paginate, show all the software or do you want both on the same page?

Lars-Janssen's avatar

@SaeedPrez I've one page that shows all the software items with pagination. And on another page I've a dropdown menu with all software items.

So currently I'm returning all software like this in my controller:

Software::paginate(15);.

So imagine there are 100 software items the dropdown only shows 15 (but off course I want to show them all here).

My question:

Should I make 2 different routes that return this:

    public function index()
    {
        return Software::all();
    }
    
    public function indexPaginated()
    {
        return Software::paginate(15);
    }

Or what would you do in this situation?

SaeedPrez's avatar
Level 50

I've one page that shows all the software items with pagination. And on another page I've a dropdown menu with all software items.

Why are two different pages using the same controller method? Are you making ajax calls?

You could do something like this..

    public function index($paginate = false)
    {
        return $paginate ? Software::paginate($paginate) : Software::all();
    }

And change your route so it can accept an optional parameter.

1 like
Lars-Janssen's avatar

@SaeedPrez Yes I'm making ajax calls. Great solution but one more question I'm using REST so Route::resource('software', 'Software\SoftwareController');

With your solution I cannot do /software anymore but I've to make /software/{pagination}. Is this good practice?

SaeedPrez's avatar

@lars64 as I wrote earlier, you have to make the parameter optional...

And generally, Route::resource() is pretty bad, I would be more explicit with my routes. For example you want the index method to accept an optional parameter, so you need it to be..

Route::get('/software/{paginate?}', 'Software\SoftwareController@index')->name('index');
1 like
devamit2018's avatar

paginate according to dropdown value in view page/////

Please or to participate in this conversation.