4

I am trying to return Json response as an array of objects. But instead I got response as object of objects. I have condition_question table where I save question_id and condition_id. I want to retrieve all questions which contains particular condition id. And sort them by answers_number. I am new to Laravel, here is my code:

 $conditionsIdArray = array($chosenConditionsIds);

        $results = Question::whereIn('question_id', function ($query) use ($conditionsIdArray) {
            $query->select('question_id')
                ->from('condition_question')
                ->whereIn('condition_id', $conditionsIdArray);
        })->get()->sortByDesc('answers_number')->take(5);

         return response()->json([
            'questions' => $results
        ], 200);

I get response:

{
    "questions": {
        "0": {
            "question_id": 842,
            "question_title": "Qui tempora...",
            "question_body": "Repellendus non sint...",
            "image": "https://lorempixel.com/640/480/?18901",
            "question_view_count": 17,
            "votes_number": 9,
            "answers_number": 9,
            "id_user": 9930,
            "created_at": "2019-09-07 09:59:05",
            "updated_at": "2019-09-08 18:23:41"
        },
        "28": {
            "question_id": 20346,
            "question_title": "Quaerat facere...",
            "question_body": "Repudiandae culpa ...",
            "image": "https://lorempixel.com/640/480/?91963",
            "question_view_count": 2,
            "votes_number": 2,
            "answers_number": 9,
            "id_user": 3546,
            "created_at": "2019-09-07 10:07:38",
            "updated_at": "2019-09-07 10:07:38"
        },etc
}

As you can see, I get object with object. I do not want key/value, just simple array of objects type Question. I am struggling two days now, have tried some different stuff like toArray() but do not know how to solve it. Any help would be most welcome.

3
  • try to edit it to response()->json($results, 200) Commented Sep 8, 2019 at 20:46
  • Why do you return as json? just return $results Commented Sep 8, 2019 at 20:47
  • Still not working as wanted. Commented Sep 8, 2019 at 20:49

1 Answer 1

5

If you change the keys in an indexed array, it will believe it has an associate array, due to sorting the index is off. On the Laravel collection there is a values() method, which you can call there that reindex the collection.

return response()->json([
    'questions' => $results->values()
], 200);
Sign up to request clarification or add additional context in comments.

5 Comments

Hi mrhn! It works, thank you! I was so close. I tried values(), but doing it in wrong way :) Thank for the link, will sure read more about it!
If it solved it, remember to accept the answer :) it is one of the quirky part of php arrays and the collection method basicly just call array_values() on the underlying array
For sure, I have to wait 3min to accept the answer :D By the way, is it possible to use $result->values() and return it as QuestionResource($result->values())?
you are dealing with a collection so you have to do QuestionResource::collection($result->values()). values() only reindexed the array and keeps it a collection so should work.
Awesome, it works perfect! Now, my Angular ngFor knows to loop :D

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.