加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel – 禁用更新时更新

发布时间:2020-12-14 19:43:38 所属栏目:大数据 来源:网络整理
导读:使用laravel 5上的查询构建器更新时遇到问题.我已尝试禁用updated_at,但仍然保持失败. 这是我的代码: $query = StockLog::where('stock_id',$id)-whereBetween('created_at',$from,$to])-update(['batch_id' = $max + 1]); 我已经尝试了两种方法: 第一个在
使用laravel 5上的查询构建器更新时遇到问题.我已尝试禁用updated_at,但仍然保持失败.

这是我的代码:

$query = StockLog::where('stock_id',$id)->whereBetween('created_at',$from,$to])->update(['batch_id' => $max + 1]);

我已经尝试了两种方法:
第一个在我的模型我设置:

public function setUpdatedAtAttribute($value)
{
    /*do nothing*/
}

第二个:

$stocklog = new StockLog;
$stocklog->timestamps = false;

$query = $stocklog::where('stock_id',[$from,$to])->update([
        'batch_id' => $max + 1]);

他们都失败了.有没有禁用updated_at?

提前致谢

By default,Eloquent will maintain the created_at and updated_at columns on your database table automatically. Simply add these timestamp columns to your table and Eloquent will take care of the rest.

我真的不建议删除它们.但是如果要使用以下方式.

将以下内容添加到您的模型中:

public $timestamps = false;

这将禁用时间戳.

编辑:看起来你想保留created_at字段,可以覆盖模型中的getUpdatedAtColumn.

使用以下代码:

public function getUpdatedAtColumn() {
    return null;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读