Laravel'非法字符串偏移'leave_form'问题与数组
Posted
技术标签:
【中文标题】Laravel\'非法字符串偏移\'leave_form\'问题与数组【英文标题】:Laravel 'Illegal string offset 'leave_form' issue with arrayLaravel'非法字符串偏移'leave_form'问题与数组 【发布时间】:2018-05-25 23:53:53 【问题描述】:我有一个编辑页面,我在其中显示休假申请表中的数据。当我更新我的表单时,我得到一个错误'非法字符串偏移'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);
【问题讨论】:
leave_form:[]
不是有效的 php 语法。错误也来自 php
已编辑 - 我从控制台复制并粘贴了它,然后进行了编辑,以便您了解数组的外观
【参考方案1】:
$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 值保存到您的记录中。
这也意味着你调用了两次更新。
【讨论】:
以上是关于Laravel'非法字符串偏移'leave_form'问题与数组的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 4 - 警告:isset 中的非法偏移类型或 \bootstrap\compiled.php 中为空