Closed csb346 closed 1 month ago
Hi, just hit the same problem. The pagination is definitely having problems with where clauses. The problem is also mentioned in #100
@csb346 I believe from your code, you're missing the where
clause in the paginate
function.
//You have this in your original query
search($term)->where('status','published')->get(); // => A collection with 12 items
//While you have this for pagination
search($term)->paginate(6);
/* From the look of things, it is valid this returns 23 items in total because it is missing the `where` clause. It should be this instead */
search($term)->where('status','published')->paginate(6)
You are right @paschaldev ! But it was only writing the issue here :) Already edited the original! Thanks. The issue remain. If i try to filter a search results the paginate isn't fully working. For now I implemented a paginate function that works on "collection". My workaround, but I would prefer a better solution.
@csb346 Yeah I read about people complaining about this issue also and the only solution was ti build your pagination manually. According to the creator, it has 0 or no runtime cost since its basically the same when you call the paginate
. Can I see how you implemented this because I'll be needing this soon thanks
@paschaldev I foud the workaround here (at the bottom:) ) : https://laracasts.com/discuss/channels/laravel/how-to-paginate-laravel-collection
You can add this method as Collection method in AppServiceProvider. just add this in boot method
if (!Collection::hasMacro('paginate')) {
Collection::macro('paginate',
function ($perPage = 15, $page = null, $options = []) {
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
return (new LengthAwarePaginator(
$this->forPage($page, $perPage), $this->count(), $perPage, $page, $options))
->withPath('');
});
}
then you can use it as usual - $items->paginate(10);
@csb346 Thanks. Got it
Im not sure why i kept having this issue, but i added the paginate macro as @csb346 mentioned above,but i kept getting no/weird results for some reason. Perhaps it was something wrong on my end.
I did some digging and found this:
$builder->get() ;// got a collection result of items as expected
//but when i tried to paginate,got no results on the same request
$builder->paginate(10);
//not sure why, but i got around this by creating a new
//collection and paginating that instead. This resulted in me getting results again.
(new Collection($builder->get()))->paginate(10);
Again perhaps this was something wrong on my end, but im simply posting this to see if anyone had similar issues.
I search for a similiar behaviour but I'm not sure that it was exactly the same issue I'm facing. Here are the results I'm getting in various tests.
Simple search: search($term)->get() => A collection with 23 items search($term)->paginate(6) =>
total: 23
lastPage: 4
items: Collection {#591 ▼
}
perPage: 6
currentPage: 1
But when I try to filter results I get this: search($term)->where('status','published')->get() => A collection with 12 items search($term)->where('status','published')->paginate(6) =>
total: 23
lastPage: 4
items: Collection {#591 ▼
}
perPage: 6
currentPage: 1
The total, last page and the results collection are clearly wrong!
Isn't that possible to do with TNT search? I was using MySQL Full text search before and Iwas hoping to find a solution in this search approach.
Is that o package limitation or bug? Or is that something I'm doing wrong? thanks guys...