kapchorwa
2 months ago
321
4
Laravel

Return a json file of only the latest records per each foreign key

Posted 2 months ago by kapchorwa

I am new to laravel and working through my first project. I have 2 tables a payments table and tenants table

payments Table => id, amount, rent_from, rent_to, created_at, updated_at , tenant_id

tenants table => .......

The tenants table has a one to many relationship with the payments table.

I need to return a json file of only the latest payments per each tenant_id.

The code

public function getPaymentsList(Request $request)
   {
$payments= DB::table("payments")
       ->where("tenant_id",$request->tenant_id)
       ->pluck("rent_to")

       ;

       return response()->json($payments);
    }

**Returns all the 'rent_to' records per tenant_id.

How can i return only the most recent rent_to record for each tenant_id as json file?

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