Paginate Laravel Collection
up vote
1
down vote
favorite
I have two different querys that I merge to return a collection
$combinados = $histMe->merge($hist)->sortByDesc('created_at');
And I return them this way:
$final = $combinados->all();
But this prints all the collection, how can I paginate this? The paginate() laravel method doesn't work with collections. And how can I controll it from the view?
Thanks!
php laravel laravel-blade laravel-collection laravel-paginate
add a comment |
up vote
1
down vote
favorite
I have two different querys that I merge to return a collection
$combinados = $histMe->merge($hist)->sortByDesc('created_at');
And I return them this way:
$final = $combinados->all();
But this prints all the collection, how can I paginate this? The paginate() laravel method doesn't work with collections. And how can I controll it from the view?
Thanks!
php laravel laravel-blade laravel-collection laravel-paginate
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have two different querys that I merge to return a collection
$combinados = $histMe->merge($hist)->sortByDesc('created_at');
And I return them this way:
$final = $combinados->all();
But this prints all the collection, how can I paginate this? The paginate() laravel method doesn't work with collections. And how can I controll it from the view?
Thanks!
php laravel laravel-blade laravel-collection laravel-paginate
I have two different querys that I merge to return a collection
$combinados = $histMe->merge($hist)->sortByDesc('created_at');
And I return them this way:
$final = $combinados->all();
But this prints all the collection, how can I paginate this? The paginate() laravel method doesn't work with collections. And how can I controll it from the view?
Thanks!
php laravel laravel-blade laravel-collection laravel-paginate
php laravel laravel-blade laravel-collection laravel-paginate
asked Nov 10 at 20:47
Alejandro Paredes
466
466
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
You can use this code that I wrote a long time ago:
You must use Length aware paginator:
use IlluminatePaginationLengthAwarePaginator;
public function paginate($items, $perPage,$setDefaultOption = true, $options = )
{
if($setDefaultOption){
$options = ['path' => request()->url(), 'query' => request()->query()];
}
$page = Input::get('page', 1); // Get the current page or default to 1
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
add a comment |
up vote
0
down vote
The api has changed slightly in newer versions. To paginate a collection you now need to use forPage.
The method forPage takes two parameters:
forPage(int $page, int $perPage)
So you can do something like this in your controller:
...
public function index(Request $request)
{
$combinados = $histMe->merge($hist)->sortByDesc('created_at');
$combinados->forPage($request->get('page'), 25);
}
...
How do I control the pagination in the blade view?
– Alejandro Paredes
Nov 10 at 23:10
@AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by apage
url parameter.
– adam
Nov 10 at 23:53
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can use this code that I wrote a long time ago:
You must use Length aware paginator:
use IlluminatePaginationLengthAwarePaginator;
public function paginate($items, $perPage,$setDefaultOption = true, $options = )
{
if($setDefaultOption){
$options = ['path' => request()->url(), 'query' => request()->query()];
}
$page = Input::get('page', 1); // Get the current page or default to 1
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
add a comment |
up vote
0
down vote
You can use this code that I wrote a long time ago:
You must use Length aware paginator:
use IlluminatePaginationLengthAwarePaginator;
public function paginate($items, $perPage,$setDefaultOption = true, $options = )
{
if($setDefaultOption){
$options = ['path' => request()->url(), 'query' => request()->query()];
}
$page = Input::get('page', 1); // Get the current page or default to 1
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use this code that I wrote a long time ago:
You must use Length aware paginator:
use IlluminatePaginationLengthAwarePaginator;
public function paginate($items, $perPage,$setDefaultOption = true, $options = )
{
if($setDefaultOption){
$options = ['path' => request()->url(), 'query' => request()->query()];
}
$page = Input::get('page', 1); // Get the current page or default to 1
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
You can use this code that I wrote a long time ago:
You must use Length aware paginator:
use IlluminatePaginationLengthAwarePaginator;
public function paginate($items, $perPage,$setDefaultOption = true, $options = )
{
if($setDefaultOption){
$options = ['path' => request()->url(), 'query' => request()->query()];
}
$page = Input::get('page', 1); // Get the current page or default to 1
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
edited Nov 10 at 21:08
answered Nov 10 at 20:59
Salar Bahador
4251313
4251313
add a comment |
add a comment |
up vote
0
down vote
The api has changed slightly in newer versions. To paginate a collection you now need to use forPage.
The method forPage takes two parameters:
forPage(int $page, int $perPage)
So you can do something like this in your controller:
...
public function index(Request $request)
{
$combinados = $histMe->merge($hist)->sortByDesc('created_at');
$combinados->forPage($request->get('page'), 25);
}
...
How do I control the pagination in the blade view?
– Alejandro Paredes
Nov 10 at 23:10
@AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by apage
url parameter.
– adam
Nov 10 at 23:53
add a comment |
up vote
0
down vote
The api has changed slightly in newer versions. To paginate a collection you now need to use forPage.
The method forPage takes two parameters:
forPage(int $page, int $perPage)
So you can do something like this in your controller:
...
public function index(Request $request)
{
$combinados = $histMe->merge($hist)->sortByDesc('created_at');
$combinados->forPage($request->get('page'), 25);
}
...
How do I control the pagination in the blade view?
– Alejandro Paredes
Nov 10 at 23:10
@AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by apage
url parameter.
– adam
Nov 10 at 23:53
add a comment |
up vote
0
down vote
up vote
0
down vote
The api has changed slightly in newer versions. To paginate a collection you now need to use forPage.
The method forPage takes two parameters:
forPage(int $page, int $perPage)
So you can do something like this in your controller:
...
public function index(Request $request)
{
$combinados = $histMe->merge($hist)->sortByDesc('created_at');
$combinados->forPage($request->get('page'), 25);
}
...
The api has changed slightly in newer versions. To paginate a collection you now need to use forPage.
The method forPage takes two parameters:
forPage(int $page, int $perPage)
So you can do something like this in your controller:
...
public function index(Request $request)
{
$combinados = $histMe->merge($hist)->sortByDesc('created_at');
$combinados->forPage($request->get('page'), 25);
}
...
answered Nov 10 at 21:14
adam
456410
456410
How do I control the pagination in the blade view?
– Alejandro Paredes
Nov 10 at 23:10
@AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by apage
url parameter.
– adam
Nov 10 at 23:53
add a comment |
How do I control the pagination in the blade view?
– Alejandro Paredes
Nov 10 at 23:10
@AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by apage
url parameter.
– adam
Nov 10 at 23:53
How do I control the pagination in the blade view?
– Alejandro Paredes
Nov 10 at 23:10
How do I control the pagination in the blade view?
– Alejandro Paredes
Nov 10 at 23:10
@AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by a
page
url parameter.– adam
Nov 10 at 23:53
@AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by a
page
url parameter.– adam
Nov 10 at 23:53
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243254%2fpaginate-laravel-collection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown