Skip to content

Commit b967f90

Browse files
Refactor date formatting in FilamentChart.php and ChartByMonth.php
1 parent aee884c commit b967f90

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/Filament/Config/FilamentChart.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@ public function __construct(
2020

2121
public static function chartBy(string $table, string $field, ?string $limit_year = null, bool $published = false): FilamentChart
2222
{
23-
$models_db = DB::table($table)
24-
->selectRaw("
23+
$is_sqlite = config('database.default') === 'sqlite';
24+
if ($is_sqlite) {
25+
$models_db = DB::table($table)
26+
->selectRaw("
27+
count(id) as total,
28+
strftime('%b %Y', {$field}) as year
29+
");
30+
} else {
31+
$models_db = DB::table($table)
32+
->selectRaw("
2533
count(id) as total,
2634
date_format({$field}, '%Y') as year
2735
");
36+
}
2837

2938
if ($published) {
3039
$models_db = $models_db->where('status', '=', PublishStatusEnum::published->value);

src/Filament/Config/FilamentChart/ChartByMonth.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,20 @@ private function setData(): Collection
142142
}
143143
}
144144

145-
$query = DB::table($this->table)
146-
->selectRaw('
145+
$is_sqlite = config('database.default') === 'sqlite';
146+
if ($is_sqlite) {
147+
$query = DB::table($this->table)
148+
->selectRaw('
149+
count(id) as total,
150+
strftime("%b %Y", '.$this->field.') as period
151+
');
152+
} else {
153+
$query = DB::table($this->table)
154+
->selectRaw('
147155
count(id) as total,
148156
date_format('.$this->field.", '%b %Y') as period
149157
");
158+
}
150159

151160
if (! empty($this->where)) {
152161
foreach ($this->where as $value) {

0 commit comments

Comments
 (0)