Viewed   707 times

I'm having some trouble with my pagination. I'm having two tables with data from a database and I paginated it with laravel Paginator.

Now my problem is when you go to, for example, page 2 it adds ?page=2 but that makes the first table go to page 2 too.

Is there anyway to get something like this ?

page_table1={number}&page_table2={number}

so you don't apply the page change on other tables.

 Answers

5

Unfortunately I can't test this code right now, but browsing at the docs and the code (in Illuminate/Pagination/Environment) I guess you could something like this:

# use default 'page' for this
$collection1 = Model::paginate(20);

# use custom 'other_page' for this
$collection2 = Model2::paginate(20);
$collection2->setPageName('other_page');

The setPageName() method isn't documented in the docs, but it's a public method alongside those indicated in the documentation, so it should be working fine. FOr reference, this is the declaration (l. 171-180 in vendor/laravel/framework/src/Illuminate/Pagination/Environment.php):

/**
 * Set the input page parameter name used by the paginator.
 *
 * @param  string  $pageName
 * @return void
 */
public function setPageName($pageName)
{
    $this->pageName = $pageName;
}

Now take into consideration that you will have another query string appended to the url, so you need to tell the pagination to consider it. Use the appends() method:

$collection1->appends(array_except(Request::query(), 'page'))->links();

$collection2->appends(array_except(Request::query(), 'other_page'))->links();

That is, tell each Presenter to build up the url with all the query strings (the array resulting from Request::query() without the current index used by the paginator, or you'll end up with a double value). array_except() is a Laravel built in array helper that returns the given array (1st parameter) purged of the passed index (2nd parameter).

I'll try to test this code as soon as I can, but it should work. Let me know in any case!

Tuesday, August 2, 2022
1

The links() (source) method is simply an alias for the render() (source) method. It seems it was introduced in Laravel 5.2.

So basically, it doesn't make a difference which you use.

Thursday, November 24, 2022
 
methai
 
1

I've same problem here, laravel 4.1 and a query scope with fulltext search. In my case the problem was the orderByRaw (a normal orderby doesn't broke pagination links).

So my "poor" solution is to keep orderByRaw off when I need a pagination and use:

->orderBy(DB::raw('my raw orderby'));

that works

Thursday, September 22, 2022
 
anijhaw
 
3

Guys I have finally used javascript to load my individual paginations into the page, so I'm sending my paginations into three different views and then using javascript to call those views into the page. Couldnt figure out any way to do multiple paginations on the same page.

Saturday, August 27, 2022
3

First of all

return View::make('frontend/search', compact('posts'))->with('success', 'Account successfully updated')->with('posts', $posts);

When doing compact you're already including the $posts var to be used as $posts in the view, exactly the same you'd do using with('posts', $posts). So this last can be removed. If you want to load more vars using compact just separe the variable names by commas.

You're paginating the item in the POST action of Search, when you click any of the pagination links or call page=2 you're not running the postSearch method, you're probably calling getSearch as you're not doing now a POST request.

Have a look to RESTful controllers

Friday, November 18, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :