Yorkata's avatar

How to make param dynamic in paginate function?

In Builder.php the paginate function contains four parameters.

 public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null)

First is the number of items per page, second is the number of the returned rows, third is the name of the pagination pages and the last one is the number of the page. My question is how to make the $page dynamic.

0 likes
10 replies
Sinnbeck's avatar

By just passing the default

->paginate(15, ['*'], 'page', $currentPage);

Or use named parameters

->paginate(page: $currentPage);
Yorkata's avatar

@Sinnbeck sorry, not sure that I fully understand. What should I put in that variable? Some sort of counter?

Sinnbeck's avatar

@Yorkata I dont understand what you mean. You pass these when you actually use it.. You cannot overwrite the defaults globally if that what you mean.

Yorkata's avatar

@Sinnbeck If I add it like that:

->paginate(1, ['*'], 'page', $currentPage);

It returns an error - that the variable is undefined. And that's what confuses me

Sinnbeck's avatar

@Yorkata Well what do you want to pass to it? Why do you want to override the page in the first place?

Yorkata's avatar

@Sinnbeck The page number. Since pages are not changing by default. When I pass a number let's say 2 it will be only on the second page so I thought if I can make it dynamic the pages will change with no problem

Sinnbeck's avatar

@Yorkata Laravel will automatically grab it from the url, if you use it as it is supposed to be used. When you click on page 2 in pagination links, it will add &page=2 to the url, which will make laravel show page 2. If this does not work, you might want to show some code, so we can help fixing it

Yorkata's avatar

@Sinnbeck When I click on page 2 or any of the other pages the URL changes (?page=1, ?page=2, ?page=6) but the page itself does not. Displays the same results no matter whats in the URL

Yorkata's avatar

@Sinnbeck Yes, sorry That's whats inside my controller

public function index()
    {   
      $competitions = Competition::orderby('id','DESC')->paginate($perPage = 1, $columns = ['*'], $pageName = 'competition');    
        return view('competition.index', compact('competitions'));   
    }

I've been stuck with this for so long

EDIT: In view:

 {{ $competitions->links() }}    

Please or to participate in this conversation.