Laravel 5 Laravel 5:更新记录,但不更改字段中更新的_的当前日期/时间值
在我的Laravel 5项目中,我希望在访客进入/刷新产品页面时增加浏览量,如:Laravel 5 Laravel 5:更新记录,但不更改字段中更新的_的当前日期/时间值,laravel-5,auto-update,Laravel 5,Auto Update,在我的Laravel 5项目中,我希望在访客进入/刷新产品页面时增加浏览量,如: $rds = Product::whereId($id)->first(); $rds->hits++; $rds->save(); 问题是:updated_at字段会自动更新,这根本不是我想要的,因为我只想更改字段:hits,而不是update_at field 如果我设置$rds->updated_at=false;该字段更新为“0000-00-00:00:0” 您能否建议如何防止仅针对特定
$rds = Product::whereId($id)->first();
$rds->hits++;
$rds->save();
问题是:updated_at字段会自动更新,这根本不是我想要的,因为我只想更改字段:hits,而不是update_at field
如果我设置$rds->updated_at=false;该字段更新为“0000-00-00:00:0”
您能否建议如何防止仅针对特定功能自动更改字段的更新内容
致以最良好的祝愿,
Naren只需在您的模型中添加以下内容,它将禁用时间戳 public$timestamps=false 或附加到方法体,如下所示 $rds->timestamps=false
注意:第一个将永久禁用时间戳,而另一个将不允许在编辑时更新时间戳。它不是
$rds->updated\u at=false
,应该是$rds->timestamps=false
将时间戳设置为false以禁用在创建和更新时创建的更新
$rds = Product::whereId($id)->first();
$rds->hits++;
$rds->timestamps = false;
$rds->save();
您可以在performUpdate
方法中查看Laravel编写的代码:
\illumb\Database\elount\Model.php
// First we need to create a fresh query instance and touch the creation and
// update timestamp on the model which are maintained by us for developer
// convenience. Then we will just continue saving the model instances.
if ($this->timestamps && Arr::get($options, 'timestamps', true)) {
$this->updateTimestamps();
}
非常感谢您的回答:)非常感谢您的快速回答如何使用$rds->hits++和select join two tables?比方说,我想将table Products与table Companys结合起来,并且我想在每次访问者来查看产品页面时更新table Products的hits字段。你能提供完整的编码吗?