i have 4 database tables want merge together, , want order data created_at field. code data still grouped per database table, result should sort of timeline of tables data. doing wrong? showing 4 items...
public function showindex() { $users = user::orderby('created_at', 'desc')->get(); $projects = project::with('votes')->orderby('created_at', 'desc')->get(); $events = calendar::orderby('created_at', 'desc')->get(); $jobs = job::orderby('created_at', 'desc')->get(); $all = $users->merge($projects)->merge($events)->merge($jobs); return view::make('users.index')->with('events', $all); }
assuming problem fact it's not order properly, can use
$all->sort(function ($a, $b) { if ($a->created_at === $b->created_at) { return 0; } return $a->created_at > $b->created_at ? 1 : -1; });
or
$all = $all->sortby(function($result) { return $result->created_at; });
see http://laravel.com/api/master/illuminate/support/collection.html#method_sort
Comments
Post a Comment