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:
@@ -134,9 +134,11 @@ class TrialPerformance extends ChartWidget
|
||||
|
||||
private function getTrialExtensionsTrend(): array
|
||||
{
|
||||
$dateFormat = $this->getDateFormatExpression();
|
||||
|
||||
return TrialExtension::query()
|
||||
->select(
|
||||
DB::raw("strftime('%Y-%m', trial_extensions.granted_at) as month"),
|
||||
DB::raw("{$dateFormat} as month"),
|
||||
DB::raw('COUNT(*) as extensions'),
|
||||
DB::raw('SUM(extension_days) as total_days')
|
||||
)
|
||||
@@ -152,6 +154,19 @@ class TrialPerformance extends ChartWidget
|
||||
->toArray();
|
||||
}
|
||||
|
||||
private function getDateFormatExpression(): string
|
||||
{
|
||||
$connection = DB::connection()->getDriverName();
|
||||
|
||||
return match ($connection) {
|
||||
'sqlite' => "strftime('%Y-%m', trial_extensions.granted_at)",
|
||||
'mysql', 'mariadb' => "DATE_FORMAT(trial_extensions.granted_at, '%Y-%m')",
|
||||
'pgsql' => "TO_CHAR(trial_extensions.granted_at, 'YYYY-MM')",
|
||||
'sqlsrv' => "FORMAT(trial_extensions.granted_at, 'yyyy-MM')",
|
||||
default => "DATE_FORMAT(trial_extensions.granted_at, '%Y-%m')", // fallback to MySQL format
|
||||
};
|
||||
}
|
||||
|
||||
private function getAverageTrialLength(): float
|
||||
{
|
||||
return Subscription::query()
|
||||
|
||||
Reference in New Issue
Block a user