Reputation: 23
I have been creating Quesionnaire where there are 3 tables : standarts,questions,and responses.
Inside each standart have their own questions.
here is how my form question look like
@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>
this is my controller :
$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);
and here is how it look like at diedump :
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 [▶]
]
While what i want is like this :
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 [▶]
]
and then insert it to the database.
im new at laravel and i have been searching the whole day on internet some example to make it work.
Thanks before
Upvotes: 0
Views: 141
Reputation: 15319
You can do somethink like this
@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
So in your controller method you can
dd($request->all()); or dd($request->standart)
Upvotes: 1