从表单循环获取数据并将其添加到数据透视表

时间:2019-03-22 13:53:40

标签: laravel eloquent

我有一个用于创建表单的刀片模板。表单使用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);
}

任何帮助表示赞赏。 保罗。

1 个答案:

答案 0 :(得分:0)

我认为这可能行得通,首先您需要像这样序列化表格:

@foreach($questions as $question)
        <div class="row mt-1">
            <div class="offset-1 col-7">
                {{$question->question}}
            </div>

            <div class="col-2">
                <label for="">Pass</label>
                <input type="radio" value="1" name="answersPass[]">
            </div>
            <div class="col-2">
                <label for=""> Fail </label>
                <input type="radio" value="0" name="answersFail[]">
            </div>
            </div>
        </div>
        <hr>
    @endforeach

然后在您的控制器中,您将能够循环所有答案,如下所示:

$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]]);
 }

我还没有测试上面的代码,所以请告诉我是否适合您。