Я достаточно испробовал все способы для успешного построения отношений 1: 1, но у меня это не получилось, поэтому я сделал это вручную, добавив столбец в таблицу миграции, которая представляет собой question_id, и затем я перешел к ответу.отправив форму, в которой уже передан идентификатор вопроса, и я добавил вход с классом hide и поместил его значение в виде $ question-> id
, а затем я просто скопировал ваше решение в цикл с ключом иработал отлично
функция администратора магазина
public function store_answer(Request $request , $id)
{
$this->validate($request,[
'answer'=>'required',
]);
$answer = new answer;
$answer->answer = $request->input('answer');
$answer->question_id = $request->input('question_id');
$answer->save();
return view('admin.questions.plus1', ['id' => $id]);
}
форма отправки администратора
@extends('layouts.admin')
@section('content')
<div>
<h1>{{$question->question}}</h1>
</div>
<div>
{!! Form::model($question,['method' => 'POST','action'=>['AdminQuestionsController@store_answer','id'=>$question->id,]]) !!}
<div class="form-group{{ $errors->has('answer') ? ' has-error' : ''}}">
{{Form::label('Answer', 'Answer')}}
{{Form::text('answer', '', ['class' => 'form-control required' , 'placeholder' => 'Enter your answer'])}}
</div>
{{Form::submit('submit',['class' => 'btn btn-primary'])}}
<div class="form-group">
{{Form::text('question_id', $question->id, ['class' => 'hide'])}}
</div>
</div>
{!! Form::close() !!}
@endsection
гостевой вид
@extends('layouts.guest')
@section('questions')
@if(count($Q) == null)
<h1>no Answerd questions yet</h1>
@else
@foreach($Q as $key => $question)
@if($question->answerd == 1)
<br>
<div class="card" style="background: linear-gradient(to right, rgb(0, 0, 0), rgb(67, 67, 67)); color:white;">
<center><h3>{{$question->question}}</h3></center></div>
<br>
<div class="card">
<center><h3>{{$A[$key]->answer}}</h3></center>
</div>
@elseif($Q->answerd == 0)
@endif
@endforeach
@endif
@endsection