Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0
Solution

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.

Last updated 2 years ago.
0

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
});
Last updated 2 years ago.
0

see the sort method

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.

Last updated 2 years ago.
0

pmall said:

see the sort method

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 :)

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

barantr90 barantr90 Joined 23 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.