Created
June 29, 2022 11:20
-
-
Save sjardim/42868220206a8427db2c112127ab4d3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Filament\Resources\ItemResource\Pages; | |
use App\Filament\Resources\ItemResource; | |
use Filament\Resources\Pages\ListRecords; | |
use Illuminate\Contracts\View\View; | |
use Illuminate\Support\Facades\DB; | |
class CustomItemsList extends ListRecords | |
{ | |
protected static string $resource = ItemResource::class; | |
protected static string $view = 'filament.resources.item-resource.pages.custom-items-list'; | |
public array $totals; | |
public function getTotals() | |
{ | |
$builder = $this->getFilteredTableQuery(); | |
$selects = []; | |
$relevantFields = []; | |
$totals = $this->getResource()::getColumnAggregation(); | |
foreach ($totals as $column => $value) { | |
switch ($value) { | |
case "sum": | |
array_push($selects, DB::raw("SUM($column) as soma_{$column}")); | |
array_push($relevantFields, "soma_{$column}"); | |
break; | |
case "count": | |
array_push($selects, DB::raw("COUNT($column) as contagem_{$column}")); | |
array_push($relevantFields, "contagem_{$column}"); | |
break; | |
case "min": | |
array_push($selects, DB::raw("MIN($column) as min_{$column}")); | |
array_push($relevantFields, "min_{$column}"); | |
break; | |
case "max": | |
array_push($selects, DB::raw("MAX($column) as max_{$column}")); | |
array_push($relevantFields, "max_{$column}"); | |
break; | |
case "range": | |
array_push($selects, DB::raw("MAX($column) as max_{$column}")); | |
array_push($selects, DB::raw("MIN($column) as min_{$column}")); | |
array_push($relevantFields, "max_{$column}"); | |
array_push($relevantFields, "min_{$column}"); | |
break; | |
case "count_distinct": | |
array_push($selects, DB::raw("COUNT(DISTINCT($column)) as count_distinct_{$column}")); | |
array_push($relevantFields, "count_distinct_{$column}"); | |
break; | |
} | |
} | |
$resultWithRelationships = $builder->select($selects)->first()->toArray(); | |
$result = array_filter($resultWithRelationships, function ($v) use ($relevantFields) { | |
return in_array($v, $relevantFields); | |
}, ARRAY_FILTER_USE_KEY); | |
$this->totals[] = $result; | |
return $result; | |
} | |
} | |
//------ | |
/* On the resource file, you need to specify the columns you want | |
public static function getColumnAggregation(): array | |
{ | |
return [ | |
"id" => "count", | |
"description" => "count_distinct", | |
"client" => "count_distinct", | |
"deadline" => "range", | |
"billed_date" => "range", | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment