Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

squiaios's avatar

How to get the last morph relation from a belongsTo relationship on morph relation

Hello every one,

I have 3 models (book, line and invoice) A book can be invoiced multiple times An invoice can have 1 or plus lines A line is attach to a model through a poyloprhic relationship

Model Books:

  • id
  • label

Model Lines:

  • id
  • object_type
  • object_id
  • invoice_id

Model Invoices:

  • id
  • billed_at
  • payment_method

Books

| ID | LABEL                 |
------------------------------
| 1  | Harry Potter          |
| 2  | The Lord of the Rings |

Lines

| ID | OBJECT_TYPE | OBJECT_ID | INVOICE_ID |
---------------------------------------------
| 1  | book        | 1         | 1          |
| 2  | book        | 2         | 1          |
| 3  | book        | 1         | 2          |

Invoices

| ID | BILLED_AT  | PAYMENT_METHOD |
------------------------------------
| 1  | 2020-02-15 | stripe         |
| 2  | 2020-08-06 | cash           |

ONLY BY USING QUERY BUILDER (no collection)

I want Books where the last line invoiced (based on billed_at column ordered DESC) has been paid with stripe

How can I do that please, I'm stuck :/

0 likes
0 replies

Please or to participate in this conversation.