In LARAVEL I have users with lots of answers with model test ids. now I want the last created answers with the group by model_test_id. here is the LARAVEL code I wrote but it gives me the first value it finds in the group and then orders by created_at. how can I get the recent data with the group clause?
$answers = auth()->user()->answers()->groupBy('model_test_id')->orderBy('created_at', 'DESC')->get();
answers
+----+--------------------------------------+---------+---------------+---------------------+
| id | uuid | user_id | model_test_id | created_at |
+----+--------------------------------------+---------+---------------+---------------------+
| 1 | 1ffcb80a-c426-4424-9fda-1cadf573289f | 1 | 11 | 2021-03-08 12:13:07 |
| 2 | 8d23ad83-0913-4beb-91f3-4851384c52d3 | 1 | 12 | 2021-03-08 12:18:33 |
| 18 | c60066e4-ea15-4b5a-82f0-d635b912bad1 | 1 | 5 | 2021-03-08 10:18:20 |
| 20 | 499e02b3-571f-444f-be34-cfc7499d3a77 | 1 | 11 | 2021-03-10 14:37:43 |
| 21 | 962f892c-83c3-4f1f-88cf-17f0a475ac07 | 1 | 12 | 2021-03-10 16:21:45 |
| 22 | ebd713f5-fdf2-4dc6-9be1-4f45a8ee71dd | 1 | 5 | 2021-03-10 17:03:07 |
+----+--------------------------------------+---------+---------------+---------------------+
output data
+----+--------------------------------------+---------+---------------+---------------------+
| id | uuid | user_id | model_test_id | created_at |
+----+--------------------------------------+---------+---------------+---------------------+
| 2 | 8d23ad83-0913-4beb-91f3-4851384c52d3 | 1 | 12 | 2021-03-08 12:18:33 |
| 1 | 1ffcb80a-c426-4424-9fda-1cadf573289f | 1 | 11 | 2021-03-08 12:13:07 |
| 18 | c60066e4-ea15-4b5a-82f0-d635b912bad1 | 1 | 5 | 2021-03-08 10:18:20 |
+----+--------------------------------------+---------+---------------+---------------------+
i want this output
+----+--------------------------------------+---------+---------------+---------------------+
| id | uuid | user_id | model_test_id | created_at |
+----+--------------------------------------+---------+---------------+---------------------+
| 22 | ebd713f5-fdf2-4dc6-9be1-4f45a8ee71dd | 1 | 5 | 2021-03-10 17:03:07 |
| 21 | 962f892c-83c3-4f1f-88cf-17f0a475ac07 | 1 | 12 | 2021-03-10 16:21:45 |
| 20 | 499e02b3-571f-444f-be34-cfc7499d3a77 | 1 | 11 | 2021-03-10 14:37:43 |
+----+--------------------------------------+---------+---------------+---------------------+