首页 > 解决方案 > Laravel:将多个数据插入数据库

问题描述

我一直在创建有 3 个表格的问卷:标准、问题和回复。
每个标准里面都有自己的问题。

这是我的表单问题的样子

           @foreach($standarts as $s)
                <form action="/standart/{{ $s->id }}/answer/post" method="post">
                @endforeach
                    @csrf
                    <div class="row pt-3 mb-3">
                        <div class="col-auto">Dokumen Pendukung :</div>
                        <div class="col-7">
                            <input class="form-control form-control-sm" name="files_link" type="text" placeholder="Link google drive" aria-label="files_link">
                        </div>
                    </div>
                    <h3 class="text-center">Penilaian</h3>
                    <table class="table table-striped table-hover table-bordered">
                        <thead>
                        <tr>
                            <th scope="col" class="text-center">No</th>
                            <th scope="col">Pertanyaan</th>
                            @foreach($standarts as $s)
                                @if($s->type == 'Likert')
                                    <th scope="col" class="text-center">1</th>
                                    <th scope="col" class="text-center">2</th>
                                    <th scope="col" class="text-center">3</th>
                                    <th scope="col" class="text-center">4</th>
                                @else
                                    <th scope="col" class="text-center">Ya</th>
                                    <th scope="col" class="text-center">Tidak</th>
                                @endif
                            @endforeach
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($standarts as $s)
                            @foreach($s->questions as $q)
                                <tr>
                                    <td style="width: 7%;" class="text-center">{{ $loop->iteration }}</td>
                                    <td>
                                        <input type="hidden" id="question" name="question[]{{ $q->id }}" value="{{$q->question}}">
                                        <input type="hidden" id="question" name="question_id[]{{ $q->id }}" value="{{$q->id}}">
                                        {{$q->question}}
                                    </td>

                                    @if($s->type == 'Likert')
                                        <td style="width: 10%;" class="text-center">
                                            <input class="form-check-input" type="radio" name="answers[]{{ $q->id }}" id="inlineRadio1" value="1">
                                        </td>
                                        <td style="width: 10%;" class="text-center">
                                            <input class="form-check-input" type="radio" name="answers[]{{ $q->id }}" id="inlineRadio2" value="2">
                                        </td>
                                        <td style="width: 10%;" class="text-center">
                                            <input class="form-check-input" type="radio" name="answers[]{{ $q->id }}" id="inlineRadio3" value="3">
                                        </td>
                                        <td style="width: 10%;" class="text-center">
                                            <input class="form-check-input" type="radio" name="answers[]{{ $q->id }}" id="inlineRadio4" value="4">
                                        </td>
                                    @else
                                        <td style="width: 10%;" class="text-center">
                                            <input class="form-check-input" type="radio" name="answers[]{{ $q->id }}" id="inlineRadio5" value="Ya">
                                        </td>
                                        <td style="width: 10%;" class="text-center">
                                            <input class="form-check-input" type="radio" name="answers[]{{ $q->id }}" id="inlineRadio6" value="Tidak">
                                        </td>
                                    @endif
                                </tr>
                            @endforeach
                        @endforeach
                        </tbody>
                    </table>
                    <div class="row pt-3 mb-4">
                        <div class="col-auto">Keterangan :</div>
                        <div class="col-10">
                            <textarea name="description" class="form-control" id="description" rows="3"></textarea>
                        </div>
                    </div>
                    <hr>
                    <div class="row pt-3 mb-4">
                        <div class="col-auto pe-0">
                            <button type="submit" class="btn btn-success">Simpan</button>
                        </div>
                        <div class="col-auto">
                            <a type="button" href="{{route('auditee.dashboard')}}" class="btn btn-secondary">Batal</a>
                        </div>
                    </div>
                </form>

这是我的控制器:

        $request->all();
        foreach ( $request->get('answers') as $answer) {
            $answers[] = [
                'user_id' => \Auth::user()->id,
                'files_link' => $request->files_link,
                'question' => $request->question,
                'question_id' => $request->question_id,
                'answer' => $request->answers,
                'description' => $request->description,
            ];
        }

        dd($answers);

这是 dieump 的样子:

array:3 [▼
  0 => array:6 [▼
    "user_id" => 3
    "files_link" => "http://127.0.0.1:8000/auditee/1/respons/"
    "question" => array:3 [▼
      0 => "Lorem Ipsum is "
      1 => "Lorem Ipsum is "
      2 => "Lorem Ipsum is "
    ]
    "question_id" => array:3 [▼
      0 => "6"
      1 => "7"
      2 => "8"
    ]
    "answer" => array:3 [▼
      0 => "Ya"
      1 => "Tidak"
      2 => "Ya"
    ]
    "description" => "asdasd"
  ]
  1 => array:6 [▶]
  2 => array:6 [▶]
]

虽然我想要的是这样的:

array:3 [▼
  0 => array:6 [▼
    "user_id" => 3
    "files_link" => "http://127.0.0.1:8000/auditee/1/respons/"
    "question" => "Lorem Ipsum is "
    "question_id" => "6"
    "answer" => "Ya"
    "description" => "asdasd"
  ]

  1 => array:6 [▼
    "user_id" => 3
    "files_link" => "http://127.0.0.1:8000/auditee/1/respons/"
    "question" => "Lorem Ipsum is"
    "question_id" => "7"
    "answer" => "Tidak"
    "description" => "asdasd"
  ]

  2 => array:6 [▶]
]

然后将其插入数据库。

我是 laravel 的新手,我整天都在互联网上搜索一些示例以使其正常工作。

之前谢谢

标签: laravellaravel-8

解决方案


你可以这样做

 @foreach($standarts as $s)
            @foreach($s->questions as $key=>$q)
                <tr>
                    <td style="width: 7%;" class="text-center">{{ $loop->iteration }}</td>
                    <td>
                        <input type="hidden" id="question" name="standart[{{$key}}][question]" value="{{$q->question}}">
                        <input type="hidden" id="question" name="standart[{{$key}}][question_id]" value="{{$q->id}}">
                        {{$q->question}}
                    </td>

                    @if($s->type == 'Likert')
                        <td style="width: 10%;" class="text-center">
                            <input class="form-check-input" type="radio" name="standart[{{$key}}][answers]" id="inlineRadio1" value="1">
                        </td>
                        <td style="width: 10%;" class="text-center">
                            <input class="form-check-input" type="radio" name="standart[{{$key}}][answers]" id="inlineRadio2" value="2">
                        </td>
                        <td style="width: 10%;" class="text-center">
                            <input class="form-check-input" type="radio" name="standart[{{$key}}][answers]" id="inlineRadio3" value="3">
                        </td>
                        <td style="width: 10%;" class="text-center">
                            <input class="form-check-input" type="radio" name="standart[{{$key}}][answers]" id="inlineRadio4" value="4">
                        </td>
                    @else
                        <td style="width: 10%;" class="text-center">
                            <input class="form-check-input" type="radio" name="standart[{{$key}}][answers]" id="inlineRadio5" value="Ya">
                        </td>
                        <td style="width: 10%;" class="text-center">
                            <input class="form-check-input" type="radio" name="standart[{{$key}}][answers]" id="inlineRadio6" value="Tidak">
                        </td>
                    @endif
                   
                </tr>
            @endforeach
        @endforeach

所以在你的控制器方法中你可以

dd($request->all()); or dd($request->standart)

推荐阅读