mercredi 20 juillet 2016

Laravel 5.1 - get data from database group by id and sum values

Im not sure how to go about this, i need to get data from a table and sum the values of each field together where the ID is the same.

Things I Have Tried

  • Pulled all data and tried to store duplicates in array then sort it like that

$users = User::all();

   $array = [];
    foreach($users as $user)
        {
            array_push($array, [$user->account_id => $user->amount]);
        }

  • Use laravel collections to store the array data and sort it through there

Other than that im not too sure how to go about this! Here is the data that i pull from the database.

0: {1: 100.00}
1: {1: 100.00}
2: {2: 100.00}
3: {2: 100.00}

This is the output i want

0: {1: 200.00}
1: {2: 200.00}

This is all i need nothing else I feel like its really simple but I have no clue, any help and guidance will be appreciated, any further information needed will be provided.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire