Hello, I have a collection of data that contains, (for example, because this comes out according to the operations of the day):
"id" => 53
"articulo_id" => 23
"almacen_id" => 1
"transtype" => "TR"
"comentario" => "Transferencia origen 5"
"trans_id" => 5
"cantidad" => -5
"onhand" => 10
"created_at" => "2022-10-17 09:04:17"
"updated_at" => "2022-10-17 09:04:17"
"articulo" => "Caja clips nro 1"
"id" => 54
"articulo_id" => 23
"almacen_id" => 2
"transtype" => "TR"
"comentario" => "Transferencia destino 5"
"trans_id" => 5
"cantidad" => 5
"onhand" => 5
"created_at" => "2022-10-17 09:04:17"
"updated_at" => "2022-10-17 09:04:17"
"articulo" => "Caja clips nro 1"
What do I get by:
$data = Operacion::join('articulos as a', 'a.id', 'operaciones.articulo_id')
->select('operaciones.*', 'a.nombre as articulo')
->whereBetween('operaciones.created_at', [$from, $to])
->when($artId > 0, function($query) use($artId){
return $query->where('articulo_id', $artId);
})
->get();
Now, I want to group by first articulo_id
and then by almacen_id
so that I have something like this in a view, (this would go inside a table):
Artículo 1
Almacén 1
created_at comentario cantidad onhand
created_at comentario cantidad onhand
created_at comentario cantidad onhand
Almacén 2
created_at comentario cantidad onhand
created_at comentario cantidad onhand
I tried to do it by
foreach ($data as $k => $operacion) {
$operaciones[$operacion['articulo_id']][$k] = $operacion['comentario'];
}
But from that I don't know how to bring me everything and then group it by store or display it in a view the way I want.
I don't know if there is any other way to achieve this, if you could help me I would really appreciate it.