通过循环关联数组更新多个雄辩模型实例

问题描述 投票:0回答:1

我有一个测验模型,代表下表:

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title       | varchar(191)     | NO   |     | NULL    |                |
| user_id     | int(11)          | NO   |     | NULL    |                |
| lesson_id   | int(11)          | NO   |     | NULL    |                |
| explanation | text             | NO   |     | NULL    |                |
| created_at  | timestamp        | YES  |     | NULL    |                |
| updated_at  | timestamp        | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+

另一个模型QuizAnswer:

+----------------+------------------+------+-----+---------+----------------+
| Field          | Type             | Null | Key | Default | Extra          |
+----------------+------------------+------+-----+---------+----------------+
| id             | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title          | varchar(191)     | NO   |     | NULL    |                |
| is_correct_ans | tinyint(1)       | NO   |     | NULL    |                |
| user_id        | int(11)          | NO   |     | NULL    |                |
| quiz_id        | int(11)          | NO   |     | NULL    |                |
| created_at     | timestamp        | YES  |     | NULL    |                |
| updated_at     | timestamp        | YES  |     | NULL    |                |
+----------------+------------------+------+-----+---------+----------------+

将这个答案视为5种选择中的一种选择。这5个答案中的每一个都属于一个测验,其中一个是正确的答案。当我存储测验时,我还在QuizController的store()方法中同时保存答案(以及是否is_correct_answer):

    public function store(QuizRequest $request)
    {

        $options = [
            'option_a' => $request->input('option_a'),
            'option_b' => $request->input('option_b'),
            'option_c' => $request->input('option_c'),
            'option_d' => $request->input('option_d'),
            'option_e' => $request->input('option_e'),
        ];
        // This variable is used for comparison
        $correctAns = $request->input('is_correct_ans');

        $quiz = new Quiz($request->all());
        auth()->user()->quizzes()->save($quiz);

        foreach ($options as $optionKey => $optionValue) {
            $answer = new QuizAnswer;
            $answer->title = $optionValue;
            $answer->user_id = auth()->id();
            $answer->quiz_id = $quiz->id;
            if($correctAns == $optionKey){
                $answer->is_correct_ans = true;
            } else {
                $answer->is_correct_ans = false;
            }
            $answer->save();
        }

        return redirect()->back();
    }

这个store()方法工作正常,但问题是当我尝试更新模型时(一次是Quiz和QuizAnswers):

    public function update(QuizRequest $request, Quiz $quiz)
    {
        // dd($request->all());
         $options = [
            'option_a' => $request->input('option_a'),
            'option_b' => $request->input('option_b'),
            'option_c' => $request->input('option_c'),
            'option_d' => $request->input('option_d'),
            'option_e' => $request->input('option_e'),
        ];
        $correctAns = $request->input('is_correct_ans');

        foreach ($options as $optionKey => $optionValue) {
            foreach ($quiz->answers as $answer) {
                $answer->title = $optionValue;
                $answer->user_id = auth()->id();
                $answer->quiz_id = $quiz->id;
                if($correctAns == $optionKey){
                    $answer->is_correct_ans = true;
                } else {
                    $answer->is_correct_ans = false;
                }
                $answer->save();
            }
        }

        $quiz->user_id = auth()->id();
        $quiz->title = $request->input('title');
        $quiz->explanation = $request->input('explanation');
        $quiz->lesson_id = $request->input('lesson_id');
        $quiz->save();

        return redirect()->back();
    }

更具体地说,我被困在for循环中。我无法更新答案,因为内部foreach循环将使用当前$optionValue更新所有答案的值。因此,如果$ options数组的最后一个值是'i',我会在quiz_answers表中得到类似的结果:

+----+-------+----------------+---------+---------+---------------------+---------------------+
| id | title | is_correct_ans | user_id | quiz_id | created_at          | updated_at          |
+----+-------+----------------+---------+---------+---------------------+---------------------+
|  1 | i     |              1 |       2 |       1 | 2017-12-22 12:25:00 | 2017-12-22 17:28:44 |
|  2 | i     |              1 |       2 |       1 | 2017-12-22 12:25:00 | 2017-12-22 17:28:44 |
|  3 | i     |              1 |       2 |       1 | 2017-12-22 12:25:00 | 2017-12-22 17:28:44 |
|  4 | i     |              1 |       2 |       1 | 2017-12-22 12:25:00 | 2017-12-22 17:28:44 |
|  5 | i     |              1 |       2 |       1 | 2017-12-22 12:25:00 | 2017-12-22 17:28:44 |
+----+-------+----------------+---------+---------+---------------------+---------------------+
php arrays laravel
1个回答
0
投票

这是我解决它的方式,感谢array_keys()

$optionKey = array_keys($options);
    $i = 0;
    foreach ($quiz->answers as $answer) {
        // foreach ($options as $optionKey => $optionValue) {
            $answer->title = $options[$optionKey[$i]];
            $answer->user_id = auth()->id();
            $answer->quiz_id = $quiz->id;
            if($correctAns == $optionKey[$i]){
                $answer->is_correct_ans = true;
            } else {
                $answer->is_correct_ans = false;
            }
            $answer->save();
            $i++;
     // }
    }
© www.soinside.com 2019 - 2024. All rights reserved.