php Laravel - 更新时禁用更新时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30319859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Disable Updated At when updating
提问by ssuhat
Get a problem with update using query builder on laravel 5. I've tried to disabled the updated_at but keep failing.
在 laravel 5 上使用查询构建器进行更新时遇到问题。我尝试禁用了 updated_at,但一直失败。
Here is my code:
这是我的代码:
$query = StockLog::where('stock_id', $id)->whereBetween('created_at', $from, $to])->update(['batch_id' => $max + 1]);
I've tried 2 ways: first one at my model i set:
我尝试了两种方法:第一个在我设置的模型中:
public function setUpdatedAtAttribute($value)
{
/*do nothing*/
}
Second one:
第二个:
$stocklog = new StockLog;
$stocklog->timestamps = false;
$query = $stocklog::where('stock_id', $id)->whereBetween('created_at', [$from, $to])->update([
'batch_id' => $max + 1]);
both of them are failed. is there anyway to disabled the updated_at?
他们两个都失败了。无论如何要禁用updated_at?
Thanks in advance
提前致谢
回答by Akar
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.
默认情况下,Eloquent 会自动维护数据库表上的 created_at 和 updated_at 列。只需将这些时间戳列添加到您的表中,Eloquent 将负责其余的工作。
I don't really suggest removing them. But if you want use the following way.
我真的不建议删除它们。但如果你想使用以下方式。
add the following to your model:
将以下内容添加到您的模型中:
public $timestamps = false;
This will disable the timestamps.
这将禁用时间戳。
EDIT: it looks like you want to keep the created_at
field, you can override the getUpdatedAtColumn
in your model.
编辑:看起来您想保留该created_at
字段,您可以覆盖getUpdatedAtColumn
模型中的 。
Use the following code:
使用以下代码:
public function getUpdatedAtColumn() {
return null;
}
回答by Bald
In your model, add this method:
在您的模型中,添加此方法:
/**
* @param mixed $value
* @return $this
*/
public function setUpdatedAt($value)
{
return $this;
}
UPDATE: In Laravel 5.5:
更新:在 Laravel 5.5 中:
Just try to use this in your model:
尝试在您的模型中使用它:
const CREATED_AT = null;
const UPDATED_AT = null;
回答by chiliNUT
The accepted answer didn't work for me, but led me in the right direction to this solution that did:
接受的答案对我不起作用,但使我朝着正确的方向找到了这个解决方案:
class Whatever extends Model {
//...
const UPDATED_AT=NULL;
//...
Laravel 5.3
Laravel 5.3
回答by vps
You can use following if you want make it off permanently.
如果你想永久关闭它,你可以使用以下方法。
Add following to your model...
将以下内容添加到您的模型中...
public $timestamps = false;
And if you want to keep using created_at, then add following.
如果您想继续使用 created_at,请添加以下内容。
static::creating( function ($model) {
$model->setCreatedAt($model->freshTimestamp());
});
OR use following way...
或使用以下方式...
/**
* Set the value of the "updated at" attribute.
*
* @param mixed $value
* @return void
*/
public function setUpdatedAt($value)
{
$this->{static::UPDATED_AT} = $value;
}
回答by Evrard-c
In this case it's better to use Query Builderinstead of Eloquentbecause Query Builderdoesn't implicitely edits timestamps fields. The use of Query Builderwill have the advantage of targetting only the concerned update operation without alterate all your model.
在这种情况下,最好使用Query Builder而不是Eloquent,因为Query Builder不会隐式编辑时间戳字段。使用Query Builder的优点是仅针对相关的更新操作,而无需更改所有模型。
In one line you could do:
在一行中,您可以执行以下操作:
$query = \DB::table('stocklogs')->where('stock_id', $id)->whereBetween('created_at', [$from, $to])->update(['batch_id' => $max + 1]);