【发布时间】:2020-08-08 04:05:18
【问题描述】:
我正在创建一个非常简单的调查网络应用程序,并且在回答创建的调查时,控制器在控制器中不起作用,而不是转储数组数据及其信息,而是直接重定向回表单,并且不会转储任何东西,执行它只是重定向回表单的代码时没有错误。我已经尝试重新迁移,并在修复了一些问题但仍然不是主要问题的视图中重写表单
调查负责人:
public function store(Questionnaire $questionnaire, $slug){
$data = request()->validate([
'responses.*.answerid' => 'required',
'responses.*.question_id' => 'required',
'survey.name' => 'required',
'survey.email' =>'required | email',
]);
dd($data);
}
Blade.php 查看:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<h1>{{ $questionnaire->title }}</h1>
<form action = "/surveys/{{ $questionnaire->id }}-{{Str::slug($questionnaire->title)}}" method = "post">
@csrf
@foreach($questionnaire->questions as $key => $question)
<div class="card mt-4">
<div class="card-header"><strong> {{ $key + 1 }} </strong>{{ $question->question }}</div>
<div class = "card-body">
@error('responses.' . $key . '.answerid')
<small class = "text-danger">{{ $message }}</small>
@enderror
<ul class = "list-group">
@foreach($question->answers as $answer)
<label for="answer{{$answer->id}}">
<li class="list-group-item">
<input type = "hidden" name = 'responses[{{$key}}][questionid]' value = '{{$question->id}}'>
<input type = "radio" name = 'responses[{{$key}}][answerid]' id = 'answer{{ $answer->id }}' class = "mr-2" value= "{{$answer->id}}" {{ old('responses.' . $key . '.answer_id') == $answer->id ? 'checked' : '' }}>
{{ $answer->answer }}
</li>
</label>
@endforeach
</ul>
</div>
</div>
@endforeach
<div class="card mt-4">
<div class="card-header">Your Information</div>
<div class="card-body">
<div class="form-group">
<label for="name">Your Name</label>
<input name = "survey[name]" type="text" class="form-control" id="name" aria-describedby="nameHelp" placeholder="Enter Name">
<small id="nameHelp" class="form-text text-muted">Please Enter Your Name</small>
@error('name')
<small class = "text-danger">{{ $message }}</small>
@enderror
</div>
</div>
<div class="card-body">
<div class="form-group">
<label for="email">Your Email</label>
<input name = "survey[email]" type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter Email">
<small id="emailHelp" class="form-text text-muted">Please enter your Email</small>
@error('email')
<small class = "text-danger">{{ $message }}</small>
@enderror
</div>
</div>
<div>
<button class = "btn btn-dark" type="submit">Complete Survey</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
@endsection
路线文件:
Route::get('/surveys/{questionnaire}-{slug}', 'SurveyController@show');
Route::post('/surveys/{questionnaire}-{slug}', 'SurveyController@store');
问卷模型:
protected $guarded = [];
public function surveys(){
return $this->hasMany(Survey::class);
}
调查模型:
protected $guarded = [];
public function questionnaire(){
return $this->belongsTo(Questionnaire::class);
}
public function responses(){
return $this->hasMany(SurveyResponse::class);
}
SurveyResponses 模型:
protected $guarded = [];
public function survey(){
return $this->belongsTo(Survey::class);
}
调查迁移:
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('questionid');
$table->unsignedBigInteger('answerid');
$table->timestamps();
});
这是blade.php文件中的表格:
<form action = "/surveys/{{ $questionnaire->id }}-{{Str::slug($questionnaire->title)}}" method = "post">
@csrf
@foreach($questionnaire->questions as $key => $question)
<div class="card mt-4">
<div class="card-header"><strong> {{ $key + 1 }} </strong>{{ $question->question }}</div>
<div class = "card-body">
@error('responses.' . $key . '.answerid')
<small class = "text-danger">{{ $message }}</small>
@enderror
<ul class = "list-group">
@foreach($question->answers as $answer)
<label for="answer{{$answer->id}}">
<li class="list-group-item">
<input type = "hidden" name = 'responses[{{$key}}][questionid]' value = '{{ $question->id }}'>
<input type = "radio" name = 'responses[{{$key}}][answerid]' id = 'answer{{ $answer->id }}' class = "mr-2" value= "{{$answer->id}}" {{ old('responses.' . $key . '.answer_id') == $answer->id ? 'checked' : '' }}>
{{ $answer->answer }}
</li>
</label>
@endforeach
</ul>
</div>
</div>
@endforeach
<div class="card mt-4">
<div class="card-header">Your Information</div>
<div class="card-body">
<div class="form-group">
<label for="name">Your Name</label>
<input name = "survey[name]" type="text" class="form-control" id="name" aria-describedby="nameHelp" placeholder="Enter Name">
<small id="nameHelp" class="form-text text-muted">Please Enter Your Name</small>
@error('name')
<small class = "text-danger">{{ $message }}</small>
@enderror
</div>
</div>
<div class="card-body">
<div class="form-group">
<label for="email">Your Email</label>
<input name = "survey[email]" type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter Email">
<small id="emailHelp" class="form-text text-muted">Please enter your Email</small>
@error('email')
<small class = "text-danger">{{ $message }}</small>
@enderror
</div>
</div>
<div>
<button class = "btn btn-dark" type="submit">Complete Survey</button>
</div>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</div>
</div>
</form>
这是应该传递问题 id 的输入:
<input type = "hidden" name = 'responses[{{$key}}][questionid]' value = '{{ $question->id }}'>
【问题讨论】:
-
您确定请求通过了验证吗?
-
它没有通过验证,但我不确定为什么问题输入字段应该发送问题 id 字段作为它的值,因为这是它在输入标签中设置的值,但是它没有发送。