首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从表单循环中获取数据并将其添加到枢轴表中

从表单循环中获取数据并将其添加到枢轴表中
EN

Stack Overflow用户
提问于 2019-03-22 13:53:39
回答 1查看 38关注 0票数 0

我有一个刀片模板来创建一个表单。表单使用foreach循环来迭代一组问题,并使用这些问题和一组单选按钮创建表单。

代码语言:javascript
复制
@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中。目前,这就是控制器的样子。

代码语言:javascript
复制
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

代码语言:javascript
复制
class VehicleCheck extends Model
{
    public function questions()
    {
        return $this->belongsToMany('App\VehicleQuestion')->withPivot('answer');
    }
}

VehicleQuestion.php

代码语言:javascript
复制
class VehicleQuestion extends Model
{
    public function check()
    {
        return $this->belongsToMany('App\VehicleCheck')->withPivot('answer');
    }
}

这是支点的迁移。

代码语言:javascript
复制
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。

我试过了

代码语言:javascript
复制
foreach($vehicle_check->questions as $questions)
{
    $vehicle_check->attach($question, ['answer' => (not sure how to access the answers in order from form);
}

任何帮助都很感激。保罗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-22 15:10:31

我认为这可能有用,首先您需要像这样序列化表单:

代码语言:javascript
复制
@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

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

代码语言:javascript
复制
$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]]);
 }

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55301134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档