duanchun1881 2017-03-10 13:38
浏览 405
已采纳

还原软删除的实体会在deleted_at列中设置无效的时间戳

I am trying to restore a soft-deleted entity in Laravel 4.2 however it keeps setting the timestamp of deleted_at column to an invalid format.

Instead of setting it to NULL it sets it to 0000-00-00 00:00:00 and it preventes me to actually restore the soft-deleted entity since Laravel keeps recognizing it as deleted because the column is not empty.

I tried with and without a setter to the deleted_at attribute as well but it does not work.

The code is actually very simple: I submit a PUT request to nullify the deleted_at column so that the user gets restored, but unsuccessfully.

// 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);
}

This is what appears in the database after restoring:

database

  • 写回答

1条回答 默认 最新

  • dqyy38265 2017-03-10 15:14
    关注

    The problem as been solved thanks to helloNL in #laravel IRC.

    Apparently JavaScript's NULL is not recognized as so in Laravel, but as an empty string.

    So I had to edit my model setter from this:

    public function setDeletedAtAttribute($value)
    {
        if ($value === NULL) {
            $this->attributes['deleted_at'] = null;
        } else {
            $this->attributes['deleted_at'] = $value;
        }
    }
    

    To this:

    public function setDeletedAtAttribute($value)
    {
        if ($value === NULL  || $value === "") {
            $this->attributes['deleted_at'] = null;
        } else {
            $this->attributes['deleted_at'] = $value;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题