0

I've got two models - Ticket & Answer, Answer is child (belongs to) of the ticket model. The goal is to sort tickets by most recent answer by its date, so in the end tickets with the recent answers should displayed at the top of the list on the frontend.

How that can be achieved?

1 Answers1

1

There are 2 solutions:

1st

Using sub-query join. Very similar case is in Laravel documentation so you can copy - paste - edit it. https://laravel.com/docs/8.x/queries#subquery-joins

2nd

You can update Ticket timestamps and order by that again similar to documentation: https://laravel.com/docs/8.x/eloquent-relationships#touching-parent-timestamps

NoOorZ24
  • 2,914
  • 1
  • 14
  • 33
  • Updating parent timestamp is pretty accurate solution for my task, thank you! – ImaFirinMahLazer Jul 26 '21 at 12:56
  • 1st case is more bullet proof as it does exactly what you asked. 2nd case is way less performance heavy on big datasets, but can be more error prone. For example it will not work with past records if you have them and editing / inserting something with raw query will not update timestamps – NoOorZ24 Jul 26 '21 at 12:56