【问题标题】:Laravel save related data in databaseLaravel 将相关数据保存在数据库中
【发布时间】:2019-12-20 07:30:14
【问题描述】:

我想使用 post form 将 question_id 保存在答案表中

我尝试使用 foreach 或函数,但我总是得到空数据

控制器

public function store(Request $request, Survey $survey)
{
    $request->validate([
        'answer' => 'required',
    ]);

    $survey->option_name = unserialize($survey->option_name);

    $answers = new Answer([
        'answer' => $request->get('answer'),
        'commentaire' => $request->get('commentaire'),
        'user_id' => auth()->id(),
        'last_ip' => request()->ip(),
        'question_id' => $survey->questions,
        'survey_id' => $survey->id,
    ]);

    $answers->save();

    return redirect()->action('SurveyController@view_survey_answers', [$survey->id]);
}

答案表 问题表所在行:

  • 身份证
  • survey_id
  • 标题
  • 时间戳

我总是得到空数据,或者我尝试使用 where 但出现 id 索引不存在等错误...

【问题讨论】:

  • 请描述survey
  • 您好,调查表:idtitledescriptionuser_id(对于未回答调查的发帖人)
  • $survey->questions 返回一个集合,而不是一个 id。您是要保存一个答案还是多个答案?请添加您的前端代码。
  • 张贴先生在答案区

标签: laravel save


【解决方案1】:

查看:

{!! Form::open(array('action'=>array('AnswerController@store', $survey->id))) !!}
          @forelse ($survey->questions as $key=>$question)
            <p class="flow-text">Question {{ $key+1 }} - {{ $question->title }}</p>
                @if($question->question_type === 'text')
                <div class="form-group">
                  <div class="input-field col s12">
                    <input id="answer" type="text" name="answer">
                    <label for="answer">Answer</label>
                  </div> 
                </div>
                @elseif($question->question_type === 'textarea')
                <div class="form-group">
                  <div class="input-field col s12">
                    <textarea id="textarea1" class="materialize-textarea" name="{{ $question->id }}[answer]"></textarea>
                    <label for="textarea1">Textarea</label>
                  </div>
                </div>
                @elseif($question->question_type === 'radio')
                  @foreach($question->option_name as $key=>$value)
                    <p style="margin:0px; padding:0px;">
                      @if($value === 'else')
                      <div class="form-group" style="margin-left: 20px;">
                        <input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{$value}}"/>
                        <label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
                        <div id="textboxes" style="display: none">
                            <br>
                            <textarea class="form-control" name="commentaire" id="exampleFormControlTextarea1" rows="3" placeholder="Write a large text here ..."></textarea>
                        </div>
                      </div>
                        @else
                      <p style="margin:0px; padding:0px;">
                        <div class="form-group" style="margin-left: 20px;">
                        <input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{ $value}}"/>
                        <label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
                        </div>
                    </p>
                        @endif
                  @endforeach
                @elseif($question->question_type === 'checkbox')
                  @foreach($question->option_name as $key=>$value)
                  <p style="margin:0px; padding:0px;">
                      <div class="form-group">
                      <input type="checkbox" id="{{ $value }}" name="answer" value="{{$value}}"/>
                      <label for="{{$value}}">{{ $value }}</label>
                      </div>
                  </p>
                  @endforeach
                @endif 
              <div class="divider" style="margin:10px 10px;"></div>
          @empty
            <span class='flow-text center-align'>Nothing to show</span>
            @endempty
          <div class="form-group">
        {{ Form::submit('Submit Survey', array('class'=>'btn btn-success mt-4')) }}
          </div>
        {!! Form::close() !!}

【讨论】:

    猜你喜欢
    • 2020-06-01
    • 2021-10-21
    • 2020-01-15
    • 2020-06-09
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 2021-07-18
    • 2021-11-24
    相关资源
    最近更新 更多