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.
try this
And that will return an array something like this:
where, if you then write this:
will return the date of the first record of that item in that store.
And so with all other fields.
How to generate the table
As for how to show it in the view as a table, I understand that you must pass that variable $articles to the view and go through it like this:
As I said in the comments, I don't know laravel or blade, but I understand that you can nest foreach in blade and go through that array.
I'm telling you all of this blindly, because I can't reproduce your environment, but a loop is a loop and I can have syntax errors, but I don't think so in terms of logic.
Try to see if it works for you as I have exposed it now.