Eddie Dane
Eddie Dane

Reputation: 1589

mapping join results as an array under related rows

In laravel am using eloquent to get data from the database.

I have two tables 'questions' and 'options' Am using the eloquent method to join 'options' to 'questions'

$questions = Question::join('options', 'options.question_id', 'questions.id');

return QuestionResource($questions);

This does return an expected collection of data, where the same question appear multiple times in the collection and each is joined with different options where the 'options.question_id' and 'question.id' are the same.

[
    {
        id: 1,
        text: "Africa is a...?",
        // joined option
        question_id: 1,
        value: "city",
        answer: false
    },
    {
        id: 1,
        text: "Africa is a...?",
        // joined option
        question_id: 1,
        value: "planet",
        answer: false
    },
    {
        id: 1,
        text: "Africa is a...?",
        // joined option
        question_id: 1,
        value: "continent",
        answer: true
    },
    {
        id: 2,
        text: "Albert Heinstein was a...?",
        // joined option
        question_id: 2,
        value: "comedian",
        answer: false
    },
    {
        id: 2,
        text: "Albert Heinstein was a...?",
        // joined option
        question_id: 1,
        value: "genius scientist",
        answer: true
    }
]

I want all options be nested under a key within the related question. Like

[
    {
        id: 1,
        text: "Africa is a...?",
        // joined options
        options: [
            {value: "city", answer: false},
            {value: "planet", answer: false},
            {value: "continent", answer: true}
        ]
    },
    {
        id: 2,
        text: "Albert Heinstein was a...?",
        // joined options
        options: [
            {value: "comedian", answer: false},
            {value: "genius scientist", answer: true}
        ]
    }
]

Can I achieve this with laravel's eloquent or I'll have to apply an extra logic.

Upvotes: 0

Views: 41

Answers (1)

M0ns1f
M0ns1f

Reputation: 2725

If you want to applie extra logic this code may help you

    <?php 
    $combinedqst = array('id' => '','text'=> '','option'=> array('value' => array('value' => ,'' ), 'answer' => ''));
    $ids = array('id' => , '');
    //loop through questions array 1st loop for getting the question
    foreach($questions as $question) {
        $count = 0;
    //check if question is already looped
        if(!in_array($question["id"],$ids)){
    //2end loop to get the opstions 
         foreach($questions as $question_check) {

                if($question_check["id"] ==  $question["id"]){
                    if($count == 0){
                     $combinedqst["id"] = $question["id"];
                     $combinedqst["text"] = $question["text"];
                    }
                     $count = 1;
                     array_push($combinedqst["option"]['value'],$question_check['value']);
                     array_push($combinedqst["option"]['answer'],$question_check['answer']);
                }

            }
        }
    array_push($ids,$question["id"]);
    }
vardump($combinedqst);

Upvotes: 1

Related Questions