0

I am trying to return a total using sum() by fetching the "duration" from each user within a $collection

The structure is

USER > CONTRACT_RESULTS > DAYS > DATE > ACTIVITIES > ACTIVITY

I want to query the "type" of activity and pull back the duration that is associated with that activity.

I had it working when it was much less nested using:

$collection->whereIn('activity',['off'])->sum('duration'); 

But I'm at a loss now since they are so nested and the names of the arrays are dynamic

Here is an example of the structure for 1 user in the $collection:

array:13 [▼
  "user_id" => 52
   "contract_results" => array:2 [▼
    "2019-10-21 - 2019-10-31" => array:10 [▼
      "hours_a_week" => "20"
      "days" => array:1 [▼
        "2019-10-21" => array:2 [▼
          "bank_holiday" => "no"
          "activities" => array:2 [▼
            "10:00 - 22:00" => array:5 [▼
              "type" => "driving"
              "from" => "10:00"
              "to" => "22:00"
              "duration" => 720
              "night_minutes" => 60
            ]
            "22:00 - 00:00" => array:5 [▼
              "type" => "driving"
              "from" => "22:00"
              "to" => "00:00"
              "duration" => 120
              "night_minutes" => 120
            ]
          ]
        ]
      ]
    ]
    "2019-11-01 - 2019-11-10" => array:10 [▼
      "hours_a_week" => "10"
      "days" => array:1 [▼
        "2019-11-01" => array:2 [▼
          "bank_holiday" => "yes"
          "activities" => array:1 [▼
            "10:00 - 22:00" => array:5 [▼
              "type" => "availability"
              "from" => "10:00"
              "to" => "22:00"
              "duration" => 720
              "night_minutes" => 60
            ]
          ]
        ]
      ]
    ]
  ]
]

I'm new to this so, thank you very much for your input!

4
  • 1
    What is activity? I don't see activity in your collection. Commented Nov 8, 2019 at 17:19
  • activity is arrays with the name set as times under the "activities" array - an example is "10:00 - 22:00" within each is type, from, to, duration, night minutes Commented Nov 8, 2019 at 17:48
  • 1
    Sorry, I don't get it. Look at your ->whereIn('activity', ['off']). I don't see activity key in your collection. Commented Nov 8, 2019 at 17:51
  • Before I had a much simpler array and there was an array called "activity" now I have a more complex nested array and there is an array called "activities" and within this lots of arrays with dynamic names like "10:00 - 22:00" these are now the new "activity" that I need to query.. if it was a loop it would be like: $duration_total = ''; for each "days" as day - for each activities as activity $duration_total .= duration Commented Nov 9, 2019 at 14:31

1 Answer 1

2

Based on your unclear information, this is all that I can do:

$durationTotal = collect($data->get('contract_results'))
        ->transform(function ($contractResult) {
            return array_merge((array) $contractResult, [
                'days' => collect(data_get($contractResult, 'days'))
                    ->transform(function ($day) {
                        return array_merge((array) $day, [
                            'activities' => collect(data_get($day, 'activities'))
                                ->whereIn('type', ['driving'])
                                ->toArray()
                        ]);
                    })
                    ->toArray()
            ]);
        })
        ->sum(function ($item) {
            return array_sum(data_get($item, 'days.*.activities.*.duration', []));
        });
Sign up to request clarification or add additional context in comments.

2 Comments

Just had chance to try it and it worked a charm - thank you so much - sorry for being unclear
Glad if it works. Feel free to ask if you'd another problem. Feel free to upvote too 👌

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.