I have two tables a payments table and a tenants table. Each payments table can have many payments for a single tenant (one to many relationship)
Payments
id tenant_id amount
1 1 5000
2 1 6000
3 2 7000
4 2 8000
tenants
id name email
1 peter [email protected]
2 grace [email protected]
When i run the following in my controller
public function getPaymentsList(Request $request)
{
$payments = DB::table("payments")
->join('tenants','tenants.id','=','tenant_id')
->select('payments.id','payments.tenant_id','tenants.name','payments.rent_to')
->groupBy('tenant_id')->get()
;
return response()->json($payments);
}
}
I get json content of the oldest record in the payments table not the latest record.
[
{
"id": 1,
"tenant_id": 1,
"name": "peter",
"amount": "5000"
},
{
"id": 3,
"tenant_id": 2,
"name": "grace",
"amount": "7000"
},
]
The output i want is
[
{
"id": 2,
"tenant_id": 1,
"name": "peter",
"amount": "6000"
},
{
"id": 4,
"tenant_id": 2,
"name": "grace",
"amount": "8000"
},
]
Can someone improve for me the above query so that it can display with the desired output. Kindly help