У меня есть шаблон лезвия, который создает форму.Форма использует цикл foreach для перебора последовательности вопросов и создает форму с этими вопросами и набором радиокнопок.
@extends('main')
@section('content')
<div class="row justify-content-center">
<h4>New Check For {{$vehicle->reg}}.</h4>
</div>
<div class="row justify-content-center">
<h5>{{$date}}</h5>
</div>
<div class="row justify-content-center">
<p><i>A fail on any check item will trigger an email to the office.</i></p>
</div>
<div class="row justify-content-center">
<p><i>Please add notes to explain faults in more detail, and to record any damage.</i></p>
</div>
</div>
<hr>
<form action="/vehicle_checks/{{$vehicle->id}}" method="post">
@csrf
<div class="row justify-content-center">
<label for="mileage"><h4><strong>Mileage</strong> <i>(previous {{$previous_mileage}})</i></h4></label>
<input class="ml-5 form-control-sm" style="border: solid 1px red" type="text" name="mileage" required="required">
</div>
<hr>
<div class="row">
<div class="offset-1 col-7">
<strong>Check</strong>
</div>
</div>
<hr>
@foreach($questions as $question)
<div class="row mt-1">
<div class="offset-1 col-7">
{{$question->question}}
</div>
<div class="col-2">
<label for="question{{$question->order}}">Pass</label>
<input type="radio" value="1" name="question{{$question->order}}">
</div>
<div class="col-2">
<label for="question{{$question->order}}">Fail</label>
<input type="radio" value="0" name="question{{$question->order}}" checked>
</div>
</div>
<hr>
@endforeach
<hr>
<div class="row">
<div class="offset-2 col-8">
<label for="notes">Notes</label>
<textarea style="width: 100%; border:solid 1px #1b1e21" rows=5 name="notes"></textarea>
</div>
</div>
<hr>
<div class="row">
<div class="offset-6 col-2"></div>
<a href="{{route('vehicle_checks.index', $vehicle->id)}}" class="btn btn-sm btn-danger">Cancel</a>
<button type="submit" class="ml-5 btn btn-sm btn-success">Save</button>
</div>
</div>
</form>
@stop
У меня проблема в том, что в контроллере мне нужно прикрепить этивопросы и ответы на Vehicle_check.В настоящее время это то, как выглядит контроллер.
public function store(Vehicle $vehicle, Request $request)
{
$check =new VehicleCheck;
$check->vehicle_id = $vehicle->id;
$check->user_id = Auth::user()->id;
$check->checked_on = Carbon::now();
$check->mileage = $request->mileage;
$check->notes = $request->notes;
$check->save();
//Code to attach question 1 to 14 and answer to the vehicle_check.
Session::flash('success', 'Check saved!!');
return redirect()->route('vehicle_checks.index', $vehicle);
}
Соотношения следующие.VehicleCheck.php
class VehicleCheck extends Model
{
public function questions()
{
return $this->belongsToMany('App\VehicleQuestion')->withPivot('answer');
}
}
VehicleQuestion.php
class VehicleQuestion extends Model
{
public function check()
{
return $this->belongsToMany('App\VehicleCheck')->withPivot('answer');
}
}
А вот миграция для сводки.
class VehicleCheckVehicleQuestion extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('vehicle_check_vehicle_question', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('vehicle_check_id')->required();
$table->unsignedInteger('vehicle_question_id')->required();
$table->boolean('answer')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
Как мне прикрепить каждый вопросс ответом на Vehicle_check.
Я пробовал это
foreach($vehicle_check->questions as $questions)
{
$vehicle_check->attach($question, ['answer' => (not sure how to access the answers in order from form);
}
Любая помощь приветствуется.Пол.