php - Laravel different objects merge and sort -


i have 2 different objects different columns in db. since need show both of objects on timeline, wanted merge them , sort date. date column not called same adding within loop.

public function index() {     $users = array_slice(wpuser::orderby('id', 'desc')->get()->toarray(), 0, 20);      // count = 80     $comments = wpcomment::with('recipe')->orderby('comment_date', 'desc')->get()->toarray();     foreach ($comments &$comment) {         $comment['date_time'] = $comment['comment_date'];     }      // count = 241     $favorites = wpfavorite::with('user', 'recipe')->orderby('date_time', 'desc')->get()->toarray();      // count = 321     $mergedarray = array_merge_recursive($comments, $favorites);      $mergedarray = collect($mergedarray);     $mergedarray = $mergedarray->sortbydesc('date_time');     $mergedarray = $mergedarray->groupby('date_time');      return $mergedarray;      //return view('dashboard.index', compact('users', 'mergedarray')); } 

the problem can't want. if leave dates of default carbon format, seem sort fine, not group day of course because of different h:m:s. if change in both models:

public function getdatetimeattribute($date) {     return carbon::createfromformat('y-m-d h:i:s', $date)->format('d.m.y.'); } 

i not getting sorted right...

i tried grouping objects before converting them array gives me sorted arrays, such have first object sorted, , merged below second object...

if have both comment , favorite posted on same date (for example today), want them both on top of json file under '02.09.2016'

you should able solve task using function modifying date_time before grouping. try following code:

$mergedarray = $mergedarray->groupby(function($item) {     return carbon::createfromformat('y-m-d h:i:s', $item['date_time'])->format('d.m.y.'); }); 

Comments