【问题标题】:update pivot table using foreach loop with dynamic form input使用带有动态表单输入的 foreach 循环更新数据透视表
【发布时间】:2016-10-14 06:53:28
【问题描述】:

请检查这张图片你会知道我需要什么LINK 我想在一个交通工具上插入许多航班,但我希望为交通工具和航班选择人员,一切都很完美 但是对于航班详细信息,如果我在我的数据透视表中选择第一次飞行中的两个人第二次飞行中的两个人,则它会在两个航班详细信息中更新 4 人的姓名。检查这个link

我想插入人名分开,例如第一个航班的详细信息是两个人,第二个航班的详细信息是两个人 所以这是我的控制器

$data = array();
        $origin = $request->get('origin');
        $destination = $request->get('destination');
        $flight_no = $request->flight_no;
        $dep_date = $request->dep_date;
        $arrival_date = $request->arrival_date;
        foreach ($origin as $key => $value){
            $data[] = [
                'transport_id' => $new->id,
                'origin' => $value,
                'destination' =>$destination[$key],
                'flight_no' =>$flight_no[$key],
                'dep_date' =>Carbon::parse($dep_date[$key])->format('Y-m-d h:i'),
                'arrival_date'=> Carbon::parse($arrival_date[$key])->format('Y-m-d h:i'),
                'user_id'=> Auth::id()
            ];
        }

        foreach($data as $d){
            $flight = Flight::create($d);
            $flight->crews()->attach($request->input('flight_crew_list'));
        }

我该怎么办?如果不清楚你需要帮助我吗? 编辑 : 我这样的表格我使用 VueJs 添加动态飞行表格

<tr v-for="row in rows">

<td>

    <ul>
        @foreach($crew_id as $key => $name)
          <li> {!! Form::checkbox('flight_crew_list[]', $key) !!}
           <b> {!! strtoupper($name) !!} </b>: ({{ $crew_type[$key] }})</li>
        @endforeach
    </ul>
</td>

<td>
    <div class="row col-md-offset-1">
        <div class="col-md-4">
            {!! Form::label('origin', 'Origin',['class'=>'control-label']) !!}
        </div>
        <div class="col-md-8">
            {!! Form::text('origin[]',null,['class' => 'input-field input-sm text-upper',]) !!}
        </div>
    </div>
    <div class="row col-md-offset-1">
        <div class="col-md-4">
            {!! Form::label('destination', 'Destination',['class'=>'control-label']) !!}

        </div>
        <div class="col-md-8">
            {!! Form::text('destination[]',null,['class' => 'input-field input-sm text-upper']) !!}
        </div>
    </div>
    <div class="row col-md-offset-1">
        <div class="col-md-4">
            {!! Form::label('flight_no', 'Flight No',['class'=>'control-label']) !!}
        </div>
        <div class="col-md-8">
            {!! Form::text('flight_no[]',null,['class' => 'input-field input-sm text-upper']) !!}
        </div>
    </div>
    <div class="row col-md-offset-1">
        <div class="col-md-4">
            {!! Form::label('datetime3', 'Departure',['class'=>'control-label']) !!}
        </div>
        <div class="col-md-8">
            {!! Form::text('dep_date[]',null,['class' => 'input-field input-sm','id'=>'datetime3']) !!}
        </div>
    </div>
    <div class="row col-md-offset-1">
        <div class="col-md-4">
            {!! Form::label('datetime4', 'Arrival Date',['class'=>'control-label']) !!}
        </div>
        <div class="col-md-8">
            {!! Form::text('arrival_date[]',null,['class' => 'input-field input-sm','id'=>'datetime4']) !!}
        </div>
    </div>
</td>
<td>
    <a @click="removeRow(row)">
    <button class="btn btn-xs " type="button" id="dim">
        <span class="glyphicon glyphicon-minus"></span>
    </button>
    </a>
</td>
</tr>

【问题讨论】:

    标签: arrays laravel dynamic foreach pivot-table


    【解决方案1】:

    目前还不清楚您的输入表单是如何制作的以及输入('flight_crew_list')中的确切内容。

    我的猜测是 input('flight_crew_list') 包含在整个表单中选择的所有机组人员 ID,这解释了为什么您将所有 4 名机组人员分配给一个航班。

    如果您想了解更多详细信息,请发布您在第一张图片中附加的表单的源代码。

    更新:

    我怀疑问题出在这里:

    {!! Form::checkbox('flight_crew_list[]', $key) !!}
    

    您正在生成包含表单中检查的所有元素的唯一数组 flight_crew_list。但是,您需要为每次飞行保留一系列经过检查的机组人员:

    {!! Form::checkbox('flight_crew_list[' . $row_id ']', $key) !!}
    

    因此,在后端之后,您只能附加针对此特定航班检查过的值。

    foreach($data as $index => $d){
        $flight = Flight::create($d);
        $flight->crews()->attach($request->input('flight_crew_list[' . $index . ']'));
    }
    

    【讨论】:

    • 嘿,谢谢,但我使用 vue-js 添加行时遇到问题,无法通过 $row_id
    • 我知道如何像这样获得 $row_id @{{ $index }} 我可以获得 id 但如何通过?有什么想法吗?
    • 在后端,您正在迭代每个来源,因此从技术上讲,您已经在后端拥有 $row_id。你不需要传递任何东西。
    猜你喜欢
    • 2019-03-24
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    相关资源
    最近更新 更多