Home > Back-end >  Laravel/PHP: Conditional Date Filter
Laravel/PHP: Conditional Date Filter

Time:10-31

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);

CodePudding user response:

Your approach is overly complex, you are unpacking and packing collections to much. Just to get the basics down. Querying your data and doing collection logic with collect(), is not interchangeable. They have different performance and logic.

Start by creating your query builder. I assumed your relation structure, pretty hard to understand based on to many conditions. Add conditional queries with when(), first parameter is condition second is the query, will not execute unless condition is true.

$startDate = $request->input('start-date');
$endDate = $request->input('end-date');

$workgroups = AnalyticsOrder::where('workgroup_id', $workgroupId)
   ->whereHas('workgroup', function ($query) use ($user) {
       $query->where('user_id', $user->id);
   })->when($startDate, function ($query) use ($startDate) {
       $query->where('created_at', '>=', $startDate);
   })->when($endDate, function ($query) use ( $endDate) {
       $query->where('created_at', '<=', $endDate);
   })->get();

You have some other parts to your logic, you have not really included, this is just a much straight forward approach with around 10 lines. Then add whatever you need on top of that.

  • Related