Do the second method. Database aggregate queries execute much faster than PHP code for the same purpose. You have to think of the following heads I am assuming you only need the sum, and not the rest of the rows
Execution Time
Your database software is better at aggregate queries than PHP. PHP will have to iterate over each instance once it has received the data to calculate the sum. Its better to do this in the database, if possible, since it is dealing with the data anyways and has optimized functions to do this sort of thing.
Data Transfer
If all you need is the sum, you are sending a number of rows, their column values that you don't really need. For the sum, you transferring just a single number. If the rowset is large, there will be significant overhead in transferring and might as well take longer to transfer the data than calculating the sum
Conditions
->where('p.user = :user')
//for example
For a where query, you again need to check in PHP if the condition matches. Additional overhead. If you need to check a foreign key relation such as where p.category in (SELECT category_id FROM user_categories as uc WHERE p.user=uc.user)
. This will require you to fetch each user's categories fromt the database iteratively to do the sum. Complicates program logic
Seperation of Concerns
The data responsibility now also lies on PHP when you can completely outsource it to the database
Where this doesn't apply: If you are displaying each row and the sum, its more sensible to calculate the same rather than shoot another query