【问题标题】:Laravel `delete()` affecting other timestamp columnsLaravel `delete()` 影响其他时间戳列
【发布时间】:2017-08-09 10:26:57
【问题描述】:

我正在使用 Laravel 软删除来“删除”一条记录。但是发生了一些奇怪的事情,用于软删除记录的delete() 命令显然会影响我表中的其他时间戳行,而它不应该影响到我的表中的其他时间戳行,除非我错过了一些关键的东西。

我的模型(仅包含相关部分,命名空间均已正确包含):

use SoftDeletes;
protected $fillable = [ 'card_id', 'expiry_date' ]; // where expiry_date is the timestamp row being affected.
protected $hidden = ['created_at', 'updated_at', 'deleted_at'];

我的控制者

public function destroy(IdCardFormRequest $request, $id)
{
    $idCard = IdCard::find($id);
    if(isset($idCard )){

        $operationStatus = $idCard ->delete();

        if($operationStatus) {
            $request->session()->flash('status', 'success');
            $request->session()->flash('message', 'Id card deleted successfully');
            return redirect()->route('admin.id-card.index');
        }
    }
}

问题
当我使用适当的路由启动删除过程时,MySql 数据行被正确地软删除,但另一个基于时间戳的行,即 expiry_date 被更新为当前时间戳。我已经阅读了 Laravel 中与软删除相关的整个文档,但没有提到其他时间戳列会受到影响。

The documentation I've read


编辑:
好吧,我已经使用dateTime() 而不是timestamp() 解决了这个问题。我仍然不确定这是一个适当的解决方案

【问题讨论】:

    标签: php laravel laravel-5 soft-delete


    【解决方案1】:

    根据您使用的数据库,日期时间和时间戳列类型可能会做不同的事情。

    例如在 MariaDB/MySQL 中,datetime 只是一种以特定格式保存日期和时间的列类型。

    timestamp 列类型也包含日期和时间,但会在添加/更新记录时自动更新,除非明确覆盖。

    来自 MariaDB 文档:

    时间戳字段通常用于定义添加或更新行的时间,默认情况下会在插入或更新记录时自动分配当前日期时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 2018-07-13
      • 2015-06-26
      相关资源
      最近更新 更多