Using Laravel I query a table called Purchasing that extracts the fields purchase_id, product_name, price, purchase_date , and returns them to a json object, everything goes well .
What I want to do is add a final property with the average of the prices of all the extracted records
The code I use in the controller is
$resultado = Compras::select("id_compra","nombre_producto","precio","fecha_compra")->where("nota_venta","=","1");
return response()->json($resultado);
The object I want to generate is like the following:
[{id_compra:1, nombre_producto:"producto 1", precio:30,fecha_compra:"2018-08-18"},
{id_compra:2, nombre_producto:"producto 2", precio:45,fecha_compra:"2018-08-18"},
{id_compra:3, nombre_producto:"producto 3", precio:80,fecha_compra:"2018-08-18"},
{id_compra:4, nombre_producto:"producto 4", precio:70,fecha_compra:"2018-08-18"},
{"promedio_precio":56.25}]
Thank you very much for your help