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:
idevakk
2025-12-02 07:09:30 -08:00
parent 659325c01d
commit e60973c391
4 changed files with 67 additions and 5 deletions

View File

@@ -92,9 +92,11 @@ class RevenueMetrics extends ChartWidget
private function getMonthlyRevenueTrend(): 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('SUM(plans.price) as revenue')
)
->join('plans', 'subscriptions.plan_id', '=', 'plans.id')
@@ -106,6 +108,19 @@ class RevenueMetrics extends ChartWidget
->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
};
}
private function getMRRByProvider(): array
{
return Subscription::query()