Paginate Laravel Collection
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 |
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 |
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 '18 at 20:47
Alejandro ParedesAlejandro Paredes
498
498
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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 |
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 '18 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 '18 at 23:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 21:08
answered Nov 10 '18 at 20:59
Salar BahadorSalar Bahador
4151313
4151313
add a comment |
add a comment |
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 '18 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 '18 at 23:53
add a comment |
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 '18 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 '18 at 23:53
add a comment |
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 '18 at 21:14
adamadam
917811
917811
How do I control the pagination in the blade view?
– Alejandro Paredes
Nov 10 '18 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 '18 at 23:53
add a comment |
How do I control the pagination in the blade view?
– Alejandro Paredes
Nov 10 '18 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 '18 at 23:53
How do I control the pagination in the blade view?
– Alejandro Paredes
Nov 10 '18 at 23:10
How do I control the pagination in the blade view?
– Alejandro Paredes
Nov 10 '18 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 '18 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 '18 at 23:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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