Group data - Laravel Collection

Asked

Viewed 519 times

0

I need to group the data of an SQL function in Indicators and Data, but in my attempt to group, I’m only getting by date(Column), for this reason the data is repeated.

Model:

public static function makeCluster($date, $flag_negocio)
{
    /** @var static $instance */
    $instance = new static;

    $table = sprintf('QLIKVIEW.dbo.FN_QLIKVIEW_PAGAMENTOS_DASHBOARD_CLUSTER_CNC(\'%s\',\'%s\')',
        $date,
        $flag_negocio
    );

    return $instance->setTable($table);
}

Controller:

$clusters = Pagamentos::makeCluster($data, $flag_negocio)
    ->selectRaw('DATA_PAGAMENTO, RAZAO_CLI, SUM(VALOR_PAGAMENTO) AS VALOR_PAGAMENTO')
    ->groupBy('RAZAO_CLI', 'DATA_PAGAMENTO')
    ->orderBy('RAZAO_CLI')
    ->get();

View:

<div class="panel panel-default">
<div class="panel-heading">
    <h4 class="panel-title text-center">Pagamento Cluster - Ronaldo</h4>
</div>

@if($clusters->isNotEmpty())
    <div class="table-responsive">
        <table class="table table-bordered table-striped table-condensed table-hover report report-sm">
            <thead>
            <tr>
                <th class="bg-info text-center">Indicador</th>
                @foreach($clusters as $key => $cluster)
                    <th class="bg-info text-center">{{ $cluster->first()->DATA_PAGAMENTO}}</th>
                @endforeach
            </tr>
            </thead>
            <tbody>
            @foreach($clusters as $cluster)
                <tr>
                    <th class="bg-info">{{ $cluster->RAZAO_CLI  }}</th>
                    @foreach($clusters as $cluster)
                        <td>{{ $cluster->first()->VALOR_PAGAMENTO ?? '&mdash;' }}</td>
                    @endforeach
                </tr>
            @endforeach
            </tbody>
        </table>

    </div>
@else
    <div class="panel-body">
        <p class="text-center text-danger">Não há dados para essa seleção.</p>
    </div>
@endif

Visual print:

inserir a descrição da imagem aqui

  • 1

    Possible duplicate of Failure in Laravel Collection 5.6?

  • The way you demonstrate the data makes it difficult to understand what you really need, and worse this is a duplicate of this https://answall.com/questions/337873/falha-no-collection-laravel-5-6 is not valid ... Try to clarify the maximum, for example which fields of this table? , because it is grouped as in the drawing, which table do you make this SQL?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.