【问题标题】:Laravel Controller Not Passing through DataLaravel 控制器不传递数据
【发布时间】: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 字段作为它的值,因为这是它在输入标签中设置的值,但是它没有发送。

标签: php laravel


【解决方案1】:

如果您的请求验证失败,您的控制器中的dd($data); 将不会运行,因为当验证失败时,它会重定向到您的视图并出现错误。所以你有两种方法可以看到错误:

1- 在您的控制器中使用以下代码:

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- 在您的视图中使用以下代码:

<!-- /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

【讨论】:

  • 谢谢,你说得对,它没有通过验证,因为问题字段是空的,blade.php 文件在问题的输入中,但我不完全确定为什么它不是通过问题 ID 发送,因为它应该是为该输入自动设置的值?
  • @KP005554 我需要查看您的刀片文件和表单代码..请粘贴代码
  • 我已经在 Blade.php 文件中添加了表单,并且我已经粘贴了应该传递实际问题 ID 的输入字段。
  • 您在答案 foreach 循环中写 &lt;input type = "hidden" name = 'responses[{{$key}}][questionid]' value = '{{ $question-&gt;id }}'&gt;,因此对于每个问题,您都有一些具有相同名称的输入。尝试将其排除在答案循环之外
  • 所以我尝试将它放在 foreach 循环的答案之外(之前和之后),我仍然收到相同的消息,即该字段没有被给出确切的消息是 -responses.0。 question_id 字段是必需的。
猜你喜欢
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 2019-04-15
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
相关资源
最近更新 更多