2.2 KiB
2.2 KiB
Walkthrough - PostgreSQL Compatibility Fixes
I have resolved the 500 Internal Server Error encountered when fetching dashboard data. The issue was caused by MySQL-specific SQL syntax in the backend controllers, which is incompatible with your PostgreSQL database.
Changes Made
Backend API
1. Statistics Controller Fix
Updated StatisticsController.php to use PostgreSQL's TO_CHAR function and double-quoted camelCase column names like "totalAmount".
Before:
$sales_data = Sale::selectRaw("DATE_FORMAT(created_at, '%Y-%m-%d') as date, SUM(totalAmount) as amount")
After:
$sales_data = Sale::selectRaw("TO_CHAR(created_at, 'YYYY-MM-DD') as date, SUM(\"totalAmount\") as amount")
2. Report Controller Fix
Updated AcnooReportController.php to use the PostgreSQL cast operator ::date and quoted camelCase columns ("saleDate", "lossProfit", etc.).
Example After:
DB::raw('"saleDate"::date as date')
DB::raw('SUM("lossProfit") as total_sale_income')
3. Other Database Compatibility Fixes
- Admin Dashboard: Replaced
MONTHNAME()withTO_CHAR(..., 'Month')in DashboardController.php. - Transfer Model: Replaced
CAST(... AS UNSIGNED)withCAST(... AS INTEGER)in Transfer.php.
Verification Results
Manual Verification Required
As the backend environment is restricted, please perform the following steps to verify the fix:
- Ensure your PostgreSQL database is running.
- Restart your Laravel development server:
php artisan serve. - Run your Flutter application and navigate to the dashboard.
- Verify that the "Failed to fetch business data 500" error no longer appears and that data is displayed correctly.