如何从控制器Laravel中的ajax对象获取数据?

时间:2017-09-07 05:04:09

标签: ajax laravel

在ajax中我有3个对象' emp_info,leave_detail和leave_items'在控制器中,我不知道如何记录每个对象。你能帮帮我吗?

$.ajax({
        type: 'POST',
        url: '/leave-request-initiator/store',
        data: {_token: $('#_token').val(), emp_info: $('#frm_emp_info').serialize(), leave_detail: $('#frm_leave_detail').serialize(), leave_items: leave_items},
        beforeSend: function () {
            console.log('sending');
        },
        success: function (response) {
            console.log(response);
        }
    });

控制器。

public function store(Request $request)
    {
        $data = $request->all();
        foreach ($data->emp_info as $item){
            $empcode = $item['EMPCODE'];
        }

        $leaveDetail = new HREmpLeave([
            'EMPCODE' => $empcode,
            'LEAVECODE' => $request->get('LEAVECODE'),
            'FROMDATE' => $request->get('FROMDATE'),
            'TODATE' => $request->get('TODATE'),
            'BACKTOWORKON' => $request->get('BACKTOWORKON'),
            'NODAY' => $request->get('NODAY'),
            'REMARK1' => $request->get('REMARK1')
        ]);
        $leaveDetail->save();
    }

1 个答案:

答案 0 :(得分:0)

改变这个:

  $data = $request->all();
  foreach ($data->emp_info as $item){
        $empcode = $item['EMPCODE'];
  }

到此:

$emp_info = parse_str($request->emp_info); //parse into a array
$empcode = $emp_info['EMPCODE'];