Laravel:发布对象数组

时间:2019-09-15 13:54:15

标签: laravel

对此代码有麻烦,我无法将dataP数组对象存储到mysql中。

Ajax请求:

$.ajax({
    url:'/register/addpostulantR',
    method:'post',
    dataType: "text",
    data:JSON.stringify(dataP),
    success(data){
        console.log("succ "+data);
    },
    error:()=>{
        console.log('error ');
    }
})

Controller:intervenantController.php

public static function addPostulantR(Request $request)
{
    return response()->json([
        'dataP' => Intervenant::addPostulantR($request->all())
    ]);
//The dataP is my json
}

型号:Intervenant.php

public static function addPostulantR($data)
{
    //$test = json_decode($data);
    $id = 0;

    foreach($data as $dataP) {
        $id = DB::table('reponse')->insertGetId($dataP);
    }

    return $id;
}

Array:来自我的javascript

let dataP = [
              {
                fk_id_quest:1,
                fk_id_post:id,
                reponse : $("#descriptif_offre").val(),
                created_at:date.getTime()/1000,
              },
              { 
                fk_id_quest:2,
                fk_id_post:id,
                reponse : $("#descriptif_offre").val(),
                created_at:date.getTime()/1000,
              }
            ];

非常感谢..

0 个答案:

没有答案