You can merge two collections
$posts = Post::orderBy('created_at', 'DESC')->get();
$org = Organization::orderBy('created_at', 'DESC')->get();
$all = $posts->merge($org);
Then you can sort it
$all_sorted = $all->sort(function($a, $b){
// classic sort callback
});
However it seems you want to make a sort of timeline, maybe another model that represents a timeline event would be suited in that case.
pmall said:
You can merge two collections
$posts = Post::orderBy('created_at', 'DESC')->get(); $org = Organization::orderBy('created_at', 'DESC')->get(); $all = $posts->merge($org);
Then you can sort it
$all_sorted = $all->sort(function($a, $b){ // classic sort callback });
Thank you so much but can u explain this, pls?
$all_sorted = $all->sort(function($a, $b){
// classic sort callback
});
However a basic understanding of php is required to use laravel at its full potential, using a callback function to sort an array is a common thing.
pmall said:
However a basic understanding of php is required to use laravel at its full potential, using a callback function to sort an array is a common thing.
thank you so much. I finally got it :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community