in Education by
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

Check

@foreach($questions as $question)
{{$question->question}}
order}}">
order}}" checked>

@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)

1 Answer

0 votes
by
I think this may work, first you will need to serialize the form like this: @foreach($questions as $question)
{{$question->question}}

@endforeach Then in your controller you will be able to loop all the answers like this: $answersPass = request('answersPass'); $answersFail = request('answersFail'); for(int i = 0 ; i < count($vehicle_check->questions) ; i++) { $vehicle_check->attach($questions[i], [answersPass[i], answersFail[i]]); } I haven't tested the code above so tell me if works for you.

Related questions

0 votes
    I have a blade template that creates a form. The form uses a foreach loop to iterate through a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    How can I display images from the storage folder (outside public)? I stored my image files in storage/ ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    We have a big system with hundreds of models (Users, Employees, Payments, Tasks, Projects, and so on ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 15, 2022 in Education by JackTerrance
0 votes
    We have a big system with hundreds of models (Users, Employees, Payments, Tasks, Projects, and so on ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    We have a big system with hundreds of models (Users, Employees, Payments, Tasks, Projects, and so on ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    We have a big system with hundreds of models (Users, Employees, Payments, Tasks, Projects, and so on ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am using datatable with Laravel to order a response that i receive from an API I connect to the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I am trying to create a pandas dataframe that combines all children into one row class Parent(Base): ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    document which is considered output after processing and by adding more data to this output it can be used as input is termed as Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    _____________ is a powerful database software that allows us to create, manage and process data in the form of a table Select the correct answer from above options...
asked Dec 20, 2021 in Education by JackTerrance
0 votes
    I have the automata S'-> S S -> a | XbY X -> ε | aZ | Y Y -> b | XX z ... JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    How to get array from form validation config file in controller? application/config/form_validation.php I have ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    How to get array from form validation config file in controller? application/config/form_validation.php I have ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I wrote javascript code to solve the following problem >>> Write a function exponentiate that accepts two ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
...