in Education by
I have two different objects with different columns in DB. Since I need to show both of those objects on timeline, I wanted to merge them and sort by date. Date column is not called the same so I am adding it within for 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 as &$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 is that I can't get what I want. If I leave dates to be of default Carbon format, they seem to sort fine, but they will not group by day of course because of different h:m:s. If I however change that in both models: public function getDateTimeAttribute($date) { return Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('d.m.Y.'); } I am not getting it sorted right... I tried by grouping the objects before even converting them to array which then gives me back sorted arrays, but such that I have first object sorted, and then merged to it below second object... If I have both Comment and Favorite posted on same date (for example today), I want them both on top of my JSON file under '02.09.2016' JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You should be able to solve your task with using function for modifying date_time before grouping. Try the following code: $mergedArray = $mergedArray->groupBy(function($item) { return Carbon::createFromFormat('Y-m-d H:i:s', $item['date_time'])->format('d.m.Y.'); });

Related questions

0 votes
    I have two different objects with different columns in DB. Since I need to show both of those objects ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    read the data from employee. text file and sort on age using counting sort,merge sort,quick sort and write ... sortedemponage.txt'. Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    I was unable to find a solution that is identical with the issue I am trying to solve. If is a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I was unable to find a solution that is identical with the issue I am trying to solve. If is a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    the _____ tools is used to draw objects with different kinds of brushes Select the correct answer from above options...
asked Dec 20, 2021 in Education by JackTerrance
0 votes
0 votes
    What are the different versions of Business Objects that you have worked on?...
asked Mar 13, 2021 in Technology by JackTerrance
0 votes
    What are the different types of database objects?...
asked Dec 18, 2020 in Technology by JackTerrance
0 votes
    HOW DO I SORT THIS BY BIRTHYEAR IN ASCENDING ORDER? I'm distance learning student and have no help. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    I have a Post model that has a likes array that contains the ObjectId of the user that liked the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    13. The sort and filter option is present under tab. * O Data O Insert O View O Page Layout Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    What exactly do you know about the Tortoise SVN and what sort of help it offers to the users?...
asked Feb 21, 2021 in Technology by JackTerrance
0 votes
    Which operation is most essential to the process of pancake sort? a) Flip the given data b) Find the largest of ... least of given data d) Inserting something into the given data...
asked Dec 28, 2022 in Technology by JackTerrance
0 votes
    What is the time complexity for a given pancake sort given it undergoes “n” flip operations? a) O(n) b) O(n2) c) O(n3) d) O(2n)...
asked Dec 28, 2022 in Technology by JackTerrance
0 votes
    How can I sort an event by id in DESC order? My all events are coming and by default they are ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
...