1

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 }}'>
2
  • Are you sure the request pass the validation? Commented Apr 24, 2020 at 14:53
  • it does not pass the validation but i am not sure why as the question input field should be sending the field of question id as its value as that is what it is set as in the input tag however it is not sending that through. Commented Apr 24, 2020 at 15:32

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you, you are correct that it is not passing the validation because the question field is empty, blade.php file in the input for the question but i'm not entirely sure why it isn't sending through the question id as it should be the value set automatically for that input?
@KP005554 I need to see your blade file and form code..please paste code
I have added the form in the blade.php file and i have pasted the input field where the actual question id should be being passed through.
You write <input type = "hidden" name = 'responses[{{$key}}][questionid]' value = '{{ $question->id }}'> in answers foreach loop so for each question you have some input with same name. Try to place that out of the answers loop
So i tried placing it outside of the answers foreach loop (both before and after) and i am still getting the same message that the field is not being given the exact message is - The responses.0.question_id field is required.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.