【问题标题】:mapping join results as an array under related rows将连接结果映射为相关行下的数组
【发布时间】:2017-09-23 06:33:30
【问题描述】:

在 laravel 中我使用 eloquent 从数据库中获取数据。

我有两个表“问题”和“选项” 我正在使用雄辩的方法将'选项'加入'问题'

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

return QuestionResource($questions);

这确实会返回预期的数据集合,其中相同的问题在集合中出现多次,并且每个问题都与不同的选项连接在一起,其中“options.question_id”和“question.id”相同。

[
    {
        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
    }
]

我希望所有选项都嵌套在相关问题中的一个键下。喜欢

[
    {
        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}
        ]
    }
]

我可以用 laravel 的 eloquent 来实现这一点,否则我将不得不应用额外的逻辑。

【问题讨论】:

  • 您需要在 PHP 中准备您的数组,只需循环您在 $questions 变量中收到的数组集合,并将结果数组作为响应传递。或者,如果您使用简单的 ORM 结构,则只需在 Question 模型中准备一个关系,您就可以使用 withload 方法或直接调用在您的集合中获取它,这是不可取的

标签: php mysql json laravel-5


【解决方案1】:

如果您想应用额外的逻辑,此代码可能会对您有所帮助

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多