I'm trying to get data back between two dates. However, these params are optional so we might get one or the other, both or neither.
I have the follow code so far, but I'm not getting any information back. Would anyone know where I'm messing up?
public function getAnalyticsData(Request $request): JsonResponse
{
$user = $request->user();
$data = collect();
$dataType = $request->input('data-type');
$suppliers = $request->input('suppliers');
$workgroupId = $request->input('workgroup-id');
$costs = $request->input('costs');
if ($dataType === 'orders') {
$ordersData = [];
foreach ($user->workgroups as $workgroup) {
if ($workgroup->id == $workgroupId) {
array_push($ordersData, collect($workgroup->analyticsOrders));
}
}
if ($request->filled('start-date') && $request->filled('end-date')) {
$ordersData = collect($ordersData)
->where('created_at', '>=', $request->input('start-date'))
->where('created_at', '<=', $request->input('end-date'));
} else if ($request->filled('start-date') || $request->filled('end-date')) {
if ($request->filled('start-date')) {
$ordersData = collect($ordersData)->where('created_at', '>=', $request->input('start-date'));
};
if ($request->filled('end-date')) {
$ordersData = collect($ordersData)->where('created_at', '<=', $request->input('end-date'));
}
}
$data->put('analyticsData', $ordersData);