Laravel'非法字符串偏移'leave_form'与数组有关

时间:2017-12-12 10:22:51

标签: javascript php arrays laravel vue.js

我有一个编辑页面,用于显示请假申请表中的数据。我更新表单时收到错误'非法字符串偏移'leave_form'。我正在使用Vue.js和PHP。我设法保存其他数据,即在leaveData对象中。当我尝试在对象数组中保存dateFrom时。 leaveData ['leave_form']然后我收到错误。我怀疑问题出在我在控制器下的foreach声明中。

我的数组(只是我从控制台复制并粘贴的示例,因此您可以看到对象键和值):

leaveData = [
alternativeName: "testos",
applicantSignature:"1",
applicantSignedDate:"2017-12-12",
contactDetailDuringLeave:"999999",
created_at:"2017-12-12 08:05:44",
created_by:6,
id:21,

leave_form = [
conditionsOfPay:"test",
created_at:"2017-12-12 08:05:44",
dateFrom:"2017-12-12",
dateTo:"2017-12-15",
id:15,
leave_application_id:21,
name:"Vacation Leave",
numberOfDays:2,
]
]

以下是我的刀片式HTML的一部分(只是leave_form部分):

             <tbody>
                <tr v-for="leave in leaveData.leave_form">
                  <th scope="row">@{{ leave.name }}</th>
                  <td><input  v-pikaday="leave.dateFrom" class="form-control" v-model="leave.dateFrom" type="text"/></td>
                  <td><input v-pikaday="leave.dateTo" class="form-control"  v-model="leave.dateTo" type="text"/></td>
                  <td><input class="form-control" type="text" name="numberOfDays" v-model="leave.numberOfDays"></td>
                  <td><input class="form-control" type="text" name="conditionsOfPay" v-model="leave.conditionsOfPay"></td>
                </tr>
              </tbody>

这是我的Vue.js:

   var leaveApply = new Vue({
el: '#leaveCreate',
data: {
  leaveData: <?php echo $leaveApplication ?>,
  getUserData: <?php echo $users ?>,
},

methods: {

  submitForm: function(){
      var that = this;

      var value = that.leaveData;

      $.ajaxSetup({
          headers: {
              'X-CSRF-TOKEN': $("#_token").attr("content")
          }
      });

      $.ajax({
      url: '/admin/internal/leave/update/' + value.id,
      type: 'POST',
      data: {
          leaveData: that.leaveData,
          personnelNumber: that.leaveData.personnelNumber,
          user_id: that.leaveData.user_id,
          alternativeName: that.leaveData.alternativeName,
          contactDetailDuringLeave: that.leaveData.contactDetailDuringLeave,
          applicantSignature: that.leaveData.applicantSignature,
          applicantSignedDate: that.leaveData.applicantSignedDate,
          managerApproval: that.leaveData.managerApproval,
          managerSignedDate: that.leaveData.managerSignedDate,
      },
      success: function(response) {
         toastr.success('Leave Application Updated');
      },
      error: function(error){
        toastr.error('Something went wrong');
      }
  });
   },
 },
   });

我的控制器:

   public function update (Request $request, $id){

  $leaveApplication = LeaveApplication::with('user','leaveDept','leaveForm', 'leaveTask')->find($id);
  $leaveApplication->personnelNumber = $request->input('personnelNumber');
  $leaveApplication->alternativeName = $request->input('alternativeName');
  $leaveApplication->contactDetailDuringLeave = $request->input('contactDetailDuringLeave');
  $leaveApplication->managerApproval = filter_var($request->input('managerApproval'), FILTER_VALIDATE_BOOLEAN);
  $leaveApplication->applicantSignature = filter_var($request->input('applicantSignature'), FILTER_VALIDATE_BOOLEAN);
  $leaveApplication->applicantSignedDate = $request->input('applicantSignedDate');
  $leaveApplication->managerSignedDate = $request->input('managerSignedDate');

  foreach($request->input('leaveData') as $leaveData){
    $leaveApplication->dateFrom = $leaveData['leave_form']['dateFrom'];
    $leaveApplication->update();
  }

  $leaveApplication->update();

  return response()->json($leaveApplication);
}

1 个答案:

答案 0 :(得分:0)

$leaveApplication = LeaveApplication::with('user','leaveDept','leaveForm', 'leaveTask')->find($id);

$leaveApplication->dateFrom = $leaveData['leave_form']['dateFrom'];

你有leave_form和LeaveForm。也许问题出在这里。还有这段代码

 foreach($request->input('leaveData') as $leaveData){
    $leaveApplication->dateFrom = $leaveData['leave_form']['dateFrom'];
    $leaveApplication->update();
  }

重复该过程并将最后一个foreach值保存到记录中。

这也意味着您正在调用更新2次。