How can I paginate a merged collection in Laravel 5?
however paginate is for eloquent models and DB queries, and not collections, it seems.
You are right. but there is ineed a paginator function for collections. forPage
Syntax:
Collection forPage(int $page, int $perPage)
Example:
Rest is simple.
public function foo()
{
$collection = collect([1,2,3,4,5,6,7,8,9,0]);
$items = $collection->forPage($_GET['page'], 5); //Filter the page var
dd($items);
}
Combine two eloquent collections into single collection and paginate
Thanks to the comments and answer posted here I managed to find a solution that seems to work:
For retrieving the data I did the following:
$posts = Post::where([
['status_id', '=', 4],
['published_at', '<=', Carbon::now()]
])->get();
$videos = Video::where([
['status_id', '=', 4],
['published_at', '<=', Carbon::now()]
])->get();
$collected = $videos->union($posts)->sortByDesc('published_at');
As mentioned in a comment on this question: https://stackoverflow.com/a/30421846/4666299 I used https://gist.github.com/simonhamp/549e8821946e2c40a617c85d2cf5af5e for the pagination.
This way I could just then do:
$items = (collect($collected))->paginate(15);
Now I have a single collection I can loop over in Blade.
Thanks for all the help!
How can i paginate laravel collection?
It is because paginate
is Builder
method, not collection.
You need to create paginator manually, how described here - https://laravel.com/docs/8.x/pagination#manually-creating-a-paginator
Eloquent paginate two relation merged
Finally found a solution :
$timeline_array = $customer->comments;
$timeline_array = $timeline_array->toBase()->merge($customer->tasks);
//sort timeline event
$timeline_array = $timeline_array->sortByDesc(function($timeline_event){
return $timeline_event->created_at;
});
$item_per_page = 10;
$timeline_array = new LengthAwarePaginator($timeline_array->forPage(Paginator::resolveCurrentPage(), $item_per_page), count($timeline_array), $item_per_page, Paginator::resolveCurrentPage(), [
'path' => Paginator::resolveCurrentPath()
]);
How to paginate a collection after get() in Laravel?
You're calling links()
and render()
on the wrong object. You've assigned the paginator to the $pagination
variable. You should be calling
$pagination->links()
or
$pagination->render()
Also, if you'd like to clean this up a little bit, you can modify your query so that you only have one query and don't need to combine two different result sets. You just need to first order on the result of the date comparison, and then order on your valid_to
field.
$events = \App\Event::orderByRaw('valid_to < ?', [$today])->orderBy('valid_to')->get();
The date comparison will return a true/false result. In ASC order (default when not specified), true
results will come after false
results, so rows where the valid_to
is less than $today
(expired) will come after the rows where valid_to
is greater than or equal to $today
.
That result set will then be ordered by the valid_to
field itself. This one query gives you the same results as the two queries you've manually merged. And, of course, you can just paginate this one query:
$events = \App\Event::orderByRaw('valid_to < ?', [$today])->orderBy('valid_to')->paginate(60);
Now, it is your $events
object that is paginated, so you would want to use $events->links()
and $events->render()
.
How to merge collection and Query builder then use paginate on it Laravel 8
Well, there is no default method available to do such things in Laravel. What you can do is you can make one by using the Illuminate\Pagination\Paginator
or Illuminate\Pagination\LengthAwarePaginator
depending on your need. Illuminate\Pagination\Paginator
refers to the simplePaginate()
method where Illuminate\Pagination\LengthAwarePaginator
refers to paginate
method.
Another way could be converting the builder result into the collection. Then merge the builder collection and the $processingRequests
collection and use forPage()
method which is also a collection method to generate pagination with some modification.
Here are the references:
Custom Pagination: https://laravel.com/docs/8.x/pagination#manually-creating-a-paginator
forPage:
https://laravel.com/docs/8.x/collections#method-forpage
https://stillat.com/blog/2018/04/22/laravel-5-collections-paginating-collections-with-forpage
Merge two sets of data and then sort and paginate in laravel
yes you can also update into single query.
$orderhenkans = Orderhenkan::where('kokyakubango',Auth::guard('web')->user()->bango)
->where('kokyakuorderbango',4)
->get()->pluck('bango');
$orderhenkans_2 = Orderhenkan::where('kokyakubango',Auth::guard('web')->user()->bango)
->get()->pluck('bango');
$misyukkos = Misyukko::where(function($query) use ($orderhenkans, $orderhenkans_2){
return $query->where(function($query_2) use ($orderhenkans){
return $query_2->whereIn('orderbango',$orderhenkans)
->whereNotNull('yoyakubi');
})
->orWhere(function($query_2) use ($orderhenkans_2){
return $query_2->whereIn('orderbango',$orderhenkans_2)
->whereNotNull('yoyakubi')
->whereDate('hanbaibukacd','<',date('Y-m-d'));
});
})->orderBy('orderbango', 'desc')->paginate(15);
edited
$orderhenkans = Orderhenkan::where('kokyakubango',Auth::guard('web')->user()->bango)
->where('kokyakuorderbango',4)
->get()->pluck('bango');
$orderhenkans_2 = Orderhenkan::where('kokyakubango',Auth::guard('web')->user()->bango)
->get()->pluck('bango');
$misyukkos = Misyukko::where(function($query) use ($orderhenkans, $orderhenkans_2){
return $query->where(function($query_2) use ($orderhenkans){
return $query_2->whereIn('orderbango',$orderhenkans);
})
->orWhere(function($query_2) use ($orderhenkans_2){
return $query_2->whereIn('orderbango',$orderhenkans_2)
->whereDate('hanbaibukacd','<',date('Y-m-d'));
});
})->whereNotNull('yoyakubi')->orderBy('orderbango', 'desc')->paginate(15);
Related Topics
Ruby and PHP Hmacs Not Agreeing
PHP Mail Special Characters Utf8
Changing Woocommerce Cart Item Names
Double Results in My Array ( MySQL_Fetch_Array )
Replace Only First Match Using Preg_Replace
What Causes an Http 405 "Invalid Method (Http Verb)" Error When Posting a Form to PHP on Iis
What Does the B in Front of String Literals Do
PHP Session for Tracking Unique Page Views
Insert Query Check If Record Exists - If Not, Insert It
Forget Password Page, Creating a Generated Password to Email to the User
Woocommerce: Display Some Reviews Randomly on Home Page
How to Query Employee Details and Relate Their Performance Metrics
PHP Datetime() Class, Change First Day of the Week to Monday
PHP How to Write Data to Middle of File Without Rewriting File