PHP , Laravel- 构造以下数据的代码

PHP , Laravel- Structure the code for the following data

我有两张桌子

questionsid(PK)name.

answerscolumns id(PK),question_id(FK),answer_!,answer_2,answer_3,answer_4,

我得到的数据是每个问题都有多个答案。

通过 Laravel.

将其插入 DB 的最佳方法是什么

我尝试过但没有成功。

您应该了解 Laravel 关系:

https://laravel.com/docs/5.3/eloquent-relationships

在您的情况下,这将是一对多关系(一个问题 - 多个答案)。

这是一个例子:请注意,没有一个代码块是实际测试过的。

数据库结构:

问题:id,name(一个问题有多个答案)

答案: id, question_id, name (一个答案属于一个问题)

我已经写了 2 个模型文件和一个 Http 控制器文件:

问题模型:

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Question Extends Model
{
    // Relation
    public function answers()
    {
        return $this->hasMany(Answer::class);
    }
}

答案模型:

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Answer Extends Model
{
    // Relation
    public function question()
    {
        return $this->belongsTo(Question::class);
    }
}

如果您想一次保存所有问题,像这样的 controller(带有 try-catches 和 transactions 等)应该会有所帮助:

原始请求的控制器

<?php
namespace App\Http\Controllers;

use App\Question;
use Illuminate\Http\Request;

class QuestionsController Extends Controller
{
    public function store(Request $request)
    {
        $questions = $request->input("question");
        $answers = $request->input("answer");
        foreach($questions as $qKey => $qName) {
            $question = Question::create(["name" => $qName]);

            if(array_key_exists($qKey, $answers) && is_array($answers[$qKey]))
            {
                foreach($answers[$qKey] as $aKey => $aName) {
                    if(! is_null($aName)) {
                        $question->answers()->create(["name" => $aName]);
                    }
                }
            }
        }
    }
}

对于可以更改请求参数的人来说,更好的方法是:

如果可以,请按如下方式更改请求

{
    "questions": [
        {
            "name": "What ever",
            "answers": [
                {"name": "answer 1 of this question"},
                {"name": "answer 2 of this question"}
            ]
        },
        {
            "name": "What ever 2",
            "answers": [
                {"name": "answer 1 of this question"},
                {"name": "answer 2 of this question"}
            ]
        }
    ]
}

这对你以后解析它们有很大帮助。

<?php
namespace App\Http\Controllers;

use App\Question;
use Illuminate\Http\Request;

class AlternateQuestionsController Extends Controller
{
    public function store(Request $request)
    {
        foreach($request->input("questions", []) as $questionPayload) {
            $question = Question::create(["name" => $questionPayload["name"]);

            foreach($questionPayload["answers"] as $answerPayload) {
                if(is_array($answerPayload)) {
                    $question->answers()->create(["name" => $answerPayload["name"]])
                }
            }
        }
    }
}

我建议一个方法它可能对你有帮助。您可以制作一个中间 table 来保持 questionanswer 模型之间的关系。

question_answer:

| answer_id | question_id |
| --------- | ----------- |
|    1      |      2      | 

并像这样检索问题的答案:

Question::find($questionId)->answer()->get();
or
Question::find($questionId)->answer;

在你的问题模型中添加这个方法:

public function answer()
    {
        return $this->belongsToMany('App\Answer','question_answer','answer_id ','question_id ');
    }

如果你看看这个页面,你会了解更多

https://laravel.com/docs/5.3/eloquent-relationships#many-to-many