Level 51
Define a new relationship latestInvoice
Subscription has many Invoices. I'm trying to retrieve Subscription with the last Invoice.
Something like this:
Subscription::whereHas('invoice', function($q){
$q->where('subscription.expires_at', 'invoice.expires_at');
})->get();
// Subscriptions
+----+------------+
| id | expires at |
+----+------------+
| 1 | 1999-12-12 |
+----+------------+
// Invoices
+----+-----------------+------------+
| id | subscription_id | expires_at|
+----+-----------------+------------+
| 5 | 1 | 1999-12-12 |
+----+-----------------+------------+
| 6 | 1 | 1998-12-12 |
+----+-----------------+------------+
| 7 | 1 | 1997-12-12 |
+----+-----------------+------------+
Subscription::whereHas('invoice', function ($q) {
$q->whereColumn('subscriptions.expires_at', 'invoices.expires_at');
})->get()
Please or to participate in this conversation.