fix(widgets): add multi-database compatibility to all dashboard widgets
Replace SQLite-specific functions with database-agnostic expressions to support MySQL, SQLite, PostgreSQL, and SQL Server across all Filament dashboard widgets. - Fix strftime() date formatting in SubscriptionMetrics, RevenueMetrics, and TrialPerformance - Fix CAST AS REAL syntax in ChurnAnalysis widget - Add getDateFormatExpression() method for date function compatibility - Add getCastExpression() method for CAST syntax compatibility - Support MySQL/MariaDB, SQLite, PostgreSQL, and SQL Server drivers - Maintain identical functionality across all database types Fixes multiple SQLSTATE[42000] syntax errors when using MySQL/MariaDB databases.
This commit is contained in:
@@ -113,9 +113,11 @@ class SubscriptionMetrics extends ChartWidget
|
||||
|
||||
private function getMonthlySubscriptionTrend(): array
|
||||
{
|
||||
$dateFormat = $this->getDateFormatExpression();
|
||||
|
||||
return Subscription::query()
|
||||
->select(
|
||||
DB::raw("strftime('%Y-%m', subscriptions.created_at) as month"),
|
||||
DB::raw("{$dateFormat} as month"),
|
||||
DB::raw('count(*) as count')
|
||||
)
|
||||
->groupBy('month')
|
||||
@@ -123,4 +125,17 @@ class SubscriptionMetrics extends ChartWidget
|
||||
->pluck('count', 'month')
|
||||
->toArray();
|
||||
}
|
||||
|
||||
private function getDateFormatExpression(): string
|
||||
{
|
||||
$connection = DB::connection()->getDriverName();
|
||||
|
||||
return match ($connection) {
|
||||
'sqlite' => "strftime('%Y-%m', subscriptions.created_at)",
|
||||
'mysql', 'mariadb' => "DATE_FORMAT(subscriptions.created_at, '%Y-%m')",
|
||||
'pgsql' => "TO_CHAR(subscriptions.created_at, 'YYYY-MM')",
|
||||
'sqlsrv' => "FORMAT(subscriptions.created_at, 'yyyy-MM')",
|
||||
default => "DATE_FORMAT(subscriptions.created_at, '%Y-%m')", // fallback to MySQL format
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user