1

The following method is intended to return an array with another array, 'data' and an Object (The result of some eloquent query).

It is however returning an array with two objects in it; $data is somehow being converted to an object with multiple child-objects, rather than being an array of objects. It should be noted that a dd($data) before the return statement reveals that it is indeed an array of objects. I think that somehow the Laravel middleware that handles response is returning this as an object instead...

Any idea how to work around this?

public function getTestData($id) {
    $participants = Participant::where('test_id', $id)->with('testRecords')->get();
    $finalRecordValue = TestRecord::where('test_id', $id)->orderBy('created_at', 'desc')->first();

    $data = [];

    foreach ($participants as $participant) {
        foreach ($participant->testRecords as $testRecord) { 
            if (!array_key_exists((int)$testRecord->capture_timestamp, $data)) {
                $data[$testRecord->capture_timestamp] = (object)[
                    'category' => $testRecord->capture_timestamp,
                    'value' . "_" . $participant->id => $testRecord->score
                ];
            } else {
                $data[$testRecord->capture_timestamp]->{"value" . "_" . $participant->id} = $testRecord->score;
            }
        }
    }

    return [$data, Auth::user()->tests()->findOrFail($id)];
}
1
  • converted to an object with multiple child-objects, its a bit vague, could you attach the dd result and expected result? Commented Aug 7, 2017 at 0:56

1 Answer 1

6

Try this before excuting return sentence or in it:

array_values($data);
Sign up to request clarification or add additional context in comments.

Comments

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.