[英]Restoring soft-deleted entities sets an invalid timestamp in the deleted_at column
我正在尝试在Laravel 4.2中恢复一个软删除的实体,但是它一直将deleted_at
列的时间戳设置为无效格式。
而不是将其设置为NULL
,而是将其设置为0000-00-00 00:00:00
,这阻止了我实际恢复软删除的实体,因为Laravel由于列不为空而一直将其识别为已删除。
我也尝试了对deleted_at
属性的设置,也没有设置,但是它不起作用。
代码实际上非常简单:我提交了一个PUT请求以使Deleted_at列无效,以便使用户恢复,但未成功。
// Javascript
$("#enable-user").on('click', function (event) {
var action = $(this).attr('action');
swal({
title: "Are you sure?",
text: "Do you want to reactivate this user?",
type: "warning",
showCancelButton: true,
html: true,
cancelButtonText: "Cancel",
confirmButtonText: "Continue",
closeOnConfirm: false,
closeOnCancel: true,
showLoaderOnConfirm: true
},
function(isConfirm) {
if (isConfirm) {
$.ajax({
type: 'PUT',
url: action,
headers: {
'X-CSRF-Token': $("input[name='_token']").attr('value')
},
data: {
deleted_at: null
},
success: function(data) {
sweetAlert("Sii!", "User re-activated successfully!", "success");
},
error: function(jqXHR, textStatus, errorThrown) {
if (jqXHR.status === 401) {
sweetAlert("Oops...", jqXHR.responseJSON.message, "error");
} else {
sweetAlert("Oops...", "Something went wrong!", "error");
}
}
});
}
});
});
// UsersController
public function updateUser($userId)
{
$input = Input::all();
$user = User::withTrashed()->where('id', '=', $userId)->firstOrFail();
if (array_key_exists('exports', $input)) {
$user->exportAccounts()->sync([]);
$user->exportAccounts()->sync($input['exports']);
unset($input['exports']);
}
if (array_key_exists('regions', $input)) {
$user->regions()->sync([]);
$user->regions()->sync($input['regions']);
unset($input['regions']);
}
$user->update($input);
$user->queueUpdateToAlgolia();
return Response::json([], 200);
}
还原后,数据库中将显示以下内容:
由于#laravel IRC中的helloNL,问题得以解决。
显然,Laravel无法识别JavaScript的NULL,而是将其识别为空字符串。
所以我必须从这里编辑我的模型设置器:
public function setDeletedAtAttribute($value)
{
if ($value === NULL) {
$this->attributes['deleted_at'] = null;
} else {
$this->attributes['deleted_at'] = $value;
}
}
对此:
public function setDeletedAtAttribute($value)
{
if ($value === NULL || $value === "") {
$this->attributes['deleted_at'] = null;
} else {
$this->attributes['deleted_at'] = $value;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.