Using Nova's ExportAsCsv action, I added a query condition to convert the timezone to limit what is returned based on that date range based on an active filter in the resource:
ExportAsCsv::make('Export')
->withFields(function ($request) {
return [
Boolean::make('Convert Timezone', 'convert_timezone')
->help('This will convert to the timezone.'),
];
})
->withQuery(function (Builder $query) use ($request) {
$date_filter = collect(json_decode(base64_decode($request->filters)))->where(
function ($filter) use ($query) {
if (!is_object($filter)) {
return false;
}
return property_exists($filter, "App\Nova\Filters\CustomDateFilter");
}
)->first()->{"App\Nova\Filters\CustomDateFilter"};
// Convert to original timezone for the report before getting the records.
if ($request->get('convert_timezone')) {
$from_tz = config('app.timezone');
$to_tz = 'GMT-7';
$query
->select(['orders.*', 'accounts.source'])
->join('accounts', 'orders.account_id', '=', 'accounts.id')
->whereRaw(
"DATE(CONVERT_TZ(orders.created_at, '$from_tz', '$to_tz')) = '$date_filter'"
);
}
})
This causes Nova to output a CSV that only contains this: Ôªø
If I remove the whereRaw condition that converts the timezone on the query, then there is no issue and the CSV exports as expected.
If I convert the query to SQL and run it manually on the DB, then it returns records like I expect.
How do I fix this so that Nova outputs the CSV data properly?