I have a blade template that creates a form. The form uses a foreach loop to iterate through a collection of questions and creates a form with these questions and a set of radio buttons.
@extends('main')
@section('content')
New Check For {{$vehicle->reg}}.
{{$date}}
A fail on any check item will trigger an email to the office.
Please add notes to explain faults in more detail, and to record any damage.
id}}" method="post">
@csrf
@foreach($questions as $question)
@endforeach
@stop
The problem I have is that in the controller I need to attach these questions and answers to a vehicle_check. Currently this is what the controller looks like.
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);
}
The relationships are as follows. 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');
}
}
And here is the migration for the pivot.
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()
{
//
}
}
How do I get to attach each question with its answer to the vehicle_check.
I've tried this
foreach($vehicle_check->questions as $questions)
{
$vehicle_check->attach($question, ['answer' => (not sure how to access the answers in order from form);
}
Any help appreciated. Paul.
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)