Selam
Model şeklinde kod şöyle
$survey = $questionnaire->surveys()->create($data['survey']);
$survey->responses()->createMany($data['responses']);
Ve bende db query ile şu şekilde yazdım
public function surveyAdd(SurveyRequest $request) {
$data = $request->validated();
$surveyId = DB::table('surveys')->insertGetId([
'questionnaire_id' => $request->input('questionnaire_id'),
'name' => $data['survey']['name'],
'email' => $data['survey']['email'],
'created_at' => now(),
'updated_at' => now(),
]);
$responsesData = [];
foreach ($data['responses'] as $response) {
$responsesData[] = [
'survey_id' => $surveyId,
'questions_id' => $response['questions_id'],
'answer_id' => $response['answer_id'],
'name' => $response['name'],
'email' => $response['email'],
'created_at' => now(),
'updated_at' => now(),
];
}
DB::table('survey_responses')->insert($responsesData);
}
Fakat bana
Undefined array key "questions_id"
'questions_id' => $response['questions_id'],
hatasını verdi.
Schema::create('surveys', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('questionnaire_id');
$table->string('name');
$table->string('email');
$table->timestamps();
});
Schema::create('survey_responses', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('survey_id');
$table->unsignedBigInteger('questions_id');
$table->unsignedBigInteger('answer_id');
$table->timestamps();
});
<form action="{{ route('surveyAdd', [$questionnaire->id, Str::slug($questionnaire->title)]) }}" method="POST">
@csrf
@foreach($questionnaire->questions as $key => $question)
<input type="hidden" name="responses[{{ $key }}][question_id]" value="{{ $question->id }}" />
<input type="hidden" name="questionnaire_id" value="{{ $questionnaire->id }}" />
<header class="card-header noborder custom-card">
<h4 class="card-title">{{ $key + 1 }}. {{ $question->question }}</h4>
</header>
<div class="card-text h-full custom-card mb-5">
<ul class="list-group custom-list-group">
@foreach($question->answers as $answer)
<label for="answer{{ $answer->answer }}">
<li class="list-group-item">
<input type="radio" name="responses[{{ $key }}][answer_id]" id="answer{{ $answer->id }}"
class="ml-2" value="{{ $answer->id }}"
{{ (old('responses.' . $key . '.answer_id') == $answer->id) ? 'checked' : '' }} />
{{ $answer->answer }}
</li>
</label>
@endforeach
</ul>
</div>
@endforeach
<div class="card-text h-full custom-card mb-5">
<div class="card-text h-full my-6 space-y-4">
<div class="input-area">
<label for="name" class="form-label">Name</label>
<input id="name" type="text" name="survey[name]" value="" class="form-control" placeholder="Name" />
</div>
<div class="input-area">
<label for="email" class="form-label">Email</label>
<input id="email" type="email" name="survey[email]" value="" class="form-control" placeholder="Email" />
</div>
</div>
</div>
<button type="submit" class="btn inline-flex justify-center btn-outline-info">Complete Survey</button>
</form>
public function rules(): array
{
return [
'responses.*.question_id' => 'required|exists:questions,id',
'responses.*.answer_id' => 'required|exists:answers,id',
'survey.name' => 'required|string|max:255',
'survey.email' => 'required|email|max:255'
];
}
Route::post('/{questionnaire}/questions/question-add', 'questionAdd')->name('questionAdd');
Blade yolladığım kodlar şöyle
public function surveyShow($questionnaireId, $slug) {
$questionnaire = DB::table('questionnaire')
->where('id', $questionnaireId)
->first();
$questionnaire->questions = DB::table('questions')
->where('questionnaire_id', $questionnaireId)
->get();
foreach ($questionnaire->questions as $question) {
$question->answers = DB::table('answers')
->where('questions_id', $question->id)
->get();
}
return view('surveys.survey-show', compact('questionnaire', 'slug'));
}