Reputation: 13
I am creating a very simple survey web app and in the answering of a created survey the controller is not functioning in the controller instead of dumping the array data and its information it instead redirects straight back to the form, and doesn't dump anything, there are no errors when executing the code it just redirects back to the form. I have tried remigrating, and rewriting the form in the view that fixed some things but still not the main issue
Survey Controller:
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 View:
@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
Routes file:
Route::get('/surveys/{questionnaire}-{slug}', 'SurveyController@show');
Route::post('/surveys/{questionnaire}-{slug}', 'SurveyController@store');
Questionnaire Model:
protected $guarded = [];
public function surveys(){
return $this->hasMany(Survey::class);
}
Survey Model:
protected $guarded = [];
public function questionnaire(){
return $this->belongsTo(Questionnaire::class);
}
public function responses(){
return $this->hasMany(SurveyResponse::class);
}
SurveyResponses Model:
protected $guarded = [];
public function survey(){
return $this->belongsTo(Survey::class);
}
Survey Migration:
Schema::create('surveys', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('questionnaire_id');
$table->string('name');
$table->string('email');
$table->timestamps();
});
Survey Responses Migration:
Schema::create('survey_responses', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('survey_id');
$table->unsignedBigInteger('questionid');
$table->unsignedBigInteger('answerid');
$table->timestamps();
});
This is the form in the blade.php file:
<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>
This is the input where the question id should be passed through:
<input type = "hidden" name = 'responses[{{$key}}][questionid]' value = '{{ $question->id }}'>
Upvotes: 1
Views: 431
Reputation: 1797
Your dd($data);
in your controller not run if your request validate fails because when validation fails it redirect to your view with errors. So you have two way to see error:
1- use below code in your controller:
use Illuminate\Support\Facades\Validator; // Important to use Validator Facade in this case
public function store(Questionnaire $questionnaire, $slug){
$validator = Validator::make($request->all(), [
'responses.*.answerid' => 'required',
'responses.*.question_id' => 'required',
'survey.name' => 'required',
'survey.email' =>'required|email',
]);
if ($validator->fails()) {
//dd($validator);
return redirect(route('route-name'))
->withErrors($validator)
->withInput();
}
}
2- use below code in your view:
<!-- /resources/views/view-name.blade.php -->
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
Upvotes: 1