Open nikuzz opened 4 years ago
Hello @nikuzz,
Currently some sub-model likeTerm
not working paginate like Product::paginate()
. I'm a little bit busy. I'll add it soon. Thanks for the patient.
Good news. New version (v3.0) released. Now you can use pagination for all. Also you can use laravel collection (filter, map, all of the method of collection and lazy collection).
If you are still facing same issue then reply with details. Thanks
Hello, pagination does work but it returns objects instead of arrays. How do i force it to return data array, with a seperate pagination object?
From version 3, By default return collection. You can return array in two ways
withOriginal
Term::withOriginal()->get()
toArray
Term::all()->toArray()
this is what im trying to do
Category::paginate();
this is what i get
{ "0":{ "id":19, "name":"Accessories", "slug":"accessories", "parent":16, "description":"" }, "1":{ "id":67, "name":"Accessories", "slug":"accessories-men", "parent":48, "description":"" }, "2":{ "id":68, "name":"Backpacks", "slug":"backpacks", "parent":48, "description":"", }, "pagination":{ "total_results":18, "total_pages":2, "current_page":1, "previous_page":null, "next_page":2, "first_page":1, "last_page":18 } }
which is an object of the data i am fetching with indexes as keys and a pagination object.
Try
$categories = Category::paginate()->toArray();
OR
$categories = Category::withOriginal()->paginate();
Is it working?
Tried both but getting a similar result.
I just fixed it. Just reinstall the package or update it to v3.0.1 or later. Now data and meta are separated.
$results = Category::paginate();
$categories = $results['data'];
$pagination = $results['meta'];
That solved it for me. Thanks for the quick assist.
Looks like there is a bug in the pagination:
$result = Category::paginate()->toArray();
the above returns correct totals and pagination data:
'meta' =>
array (size=7)
'total_results' => int 85
'total_pages' => int 9
'current_page' => int 1
'previous_page' => null
'next_page' => int 2
'first_page' => int 1
'last_page' => int 85
It also returns 10 items which is correct.
But adding ANY parameter will break the pagination:
$options = [
'page' => 1
];
$result = Category::paginate($options)->toArray();
the above returns:
'meta' =>
array (size=7)
'total_results' => int 85
'total_pages' => int 85
'current_page' => int 1
'previous_page' => null
'next_page' => int 2
'first_page' => int 1
'last_page' => int 85
Plus it only returns first item from all 85 items, doesn't matter if I set page
to 1 or 5 or 9 ... or even per_page
to 100.
PS: WooCommerce:all works OK, so I'm using it as a workaround for this scenario.
But adding ANY parameter will break the pagination:
$options = [ 'page' => 1 ]; $result = Category::paginate($options)->toArray();
This is because the paginate method has three (default) arguments, and the $options
argument comes last.
protected function paginate($per_page = 10, $current_page = 1, $options = [])
That's correct & thank you, I should've checked the code first as I haven't seen these arguments in the docs
Hi, I'm trying to get all the terms of an attribute, but with the pagination of Wordpress I can't reach the goal with the following simple statement:
$myterms=Term::all($attribute_id);
I try with the following:
Maybe there is a more elegant way?