dlebedef
329
14
Eloquent

Order by oldest entry form different relationships

Posted 4 months ago by dlebedef

Here is my case:

I have a table users. Each user may have many orders, many delivery_orders and many invoices.

Let's make it simple and say the tables are like this:

Users

  • id
  • full_name

Orders

  • id
  • user_id
  • date

Delivery Orders

  • id
  • user_id
  • date

Invoices

  • id
  • user_id
  • date

For each user I want the oldest entry (date) between orders, delivery_orders, invoices and then order all my users based on their relative oldest date.

I can achieve this by retrieving all my users and then do the filtering and ordering but is it somehow possible to do that through an eloquent request? What is the best and cleanest way to achieve this result?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.