How I can return an object instead of an array

Good day.

I am trying to return a M object instead of a M array.

See controller below: -

$query = $this->P->find();
$amounts_paid=$this->P->findTotalPaid($query);

See model below: -

public function findTotalPaid(Query $query)
{
	$query= $this->find()
			->contain(['M'])
			->select(['M.Id','amount_paid' => $query->func()->sum('amount')])
			->where(function ($exp){
				return $exp
					->isNotNull('amount');
			})
			->group('M.Id');		
	return $query;
	
}

See view below: -

            <td><?= h($amount->m['Id']) ?></td>
            <td><?= h($amount->amount_paid) ?></td>
			<!--<td><= h($amount['amount_paid']) ?></td>-->
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>-->

The problem is that I get an array with the m id in it instead of a m object.

Someone please tell me how I can get a m object instead of an m array.

findTotalPaid returns a query object. So somewhere else you call toArray() if you get an array at the view.