I have a table with a series of numeric columns and I need to return via Eloquent a column that is the sum of three fields of the same table, then I show you the structure of the table and what I need to return.
How should I build the query via Eloquent so that it returns all the records but also generates a column with the sum of the columns C1_01, C1_02 and C1_03 called totPartial, for example?
$tbSnej1 = Snejtabsnej1::paginate(5);
$totParcial = 0;
foreach ($tbSnej1 as $it){
$totParcial = ($it->C1_01 +$it->C1_02 +$it->C1_03 );
//aquí debería armar el array con todos los campos mas $totParcial
//¿como debería armar el array para devolverlo en el return?
}
This is what I was trying to do, i.e. retrieve the records and then using a foreach loop through and build the summed field I need. Now, I'm stuck on how to build an associative array to return this structure as Eloquent itself would return it.
I believe that from SQL you can solve the need raised, carrying out the following:
SUM
we obtain the calculated value in a new column to which we can give an aliasselectRaw