如何更新具有多个枢轴列的枢纽表

时间:2019-08-21 18:45:32

标签: php laravel laravel-5 many-to-many pivot-table

我有3张桌子; periodesantriperiode_santri(数据透视表)。我想更新它时遇到麻烦,因为我有多个列枢纽

我的控制器是这样的:

public function editangka(Request $request, $id)
{
    $santri = Santri::find($id);
    $data = [
        'kbm_h'=>$request->value1,
        'kbm_i'=>$request->value2,
        'kbm_a'=>$request->value3,
        'kbm_t'=>$request->value4,
        'kbm_s'=>$request->value5,
        'kbm_ct_ss'=>$request->value6,
        'kbm_jum_sam'=>$request->value7,
        'kbm_min'=>$request->value8,
        'kgt_h'=>$request->value9,
        'kgt_i'=>$request->value10,
        'kgt_a3_a5'=>$request->value11,
        'kgt_a4_a6'=>$request->value12,
        'kgt_d3'=>$request->value13,
        'kgt_a8'=>$request->value14,
        'kgt_t'=>$request->value15,
        'kgt_nomin'=>$request->value16,
        'kgt_jum_sam'=>$request->value17,
        'kgt_min'=>$request->value18,
        'pkt_yes'=>$request->value19,
        'pkt_no'=>$request->value20,
        'pkt_total'=>$request->value21,
        'pkt_min'=>$request->value22,
        'pu_kode'=>$request->value23,
        'pu_min'=>$request->value24,
        'tot_min_kbm'=>$request->value25,
        'tot_min_kgt'=>$request->value26,
        'tot_min_pkt'=>$request->value27,
        'tot_min_pu'=>$request->value28, 
        'saldo_poin'=>$request->value29, 
        'ket'=>$request->value30
    ];
    $santri->periode()->UpdateExistingPivot($request->pk, $data);
}

然后,我收到的消息是这样的:

"line": 55,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
},enter code here
{
"file": "C:\\xampp\\htdocs\\pipastibisa\\server.php",
"line": 21,
"function": "require_once"
}
]
}

1 个答案:

答案 0 :(得分:0)

您要将多个单元素数组传递给UpdateExistingPivot()方法,而第二个参数应仅是一个。

这是方法签名:

public function updateExistingPivot($id, array $attributes, $touch = true) {/** ... */}

如您所见,第一个参数是键,然后第二个参数是一个数组,其中包含所有要更新的数据。最后一个参数是触摸(更新updated_at字段)父模型。

让我为您修复它:

public function editangka(Request $request, $id)
{
    $data = [
        'kbm_h' => $request->value1,
        'kbm_i' => $request->value2,
        'kbm_a' => $request->value3,

         // ... all of your fields..

        'ket'   => $request->valueN
    ];

    $santri->periode()->UpdateExistingPivot($request->pk, $data);
}   //                                                    ^^^^^