php 禁用 Laravel 的 Eloquent 时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19937565/
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
Disable Laravel's Eloquent timestamps
提问by projectxmatt
I'm in the process of converting one of our web applications from CodeIgniter to Laravel. However at this moment we don't want to add the updated_at
/ created_at
fields to all of our tables as we have a logging class that does all this in more depth for us already.
我正在将我们的一个 Web 应用程序从 CodeIgniter 转换为 Laravel。然而,此时我们不想将updated_at
/created_at
字段添加到我们所有的表中,因为我们有一个日志类已经为我们更深入地完成了所有这些工作。
I'm aware I can set $timestamps = false;
in:
我知道我可以设置$timestamps = false;
:
Vendor\laravel\framework\src\illuminate\Datebase\Eloquent\Model.php
However I'd rather not change a core file for Laravel, or have everyone of my models have that at the top. Is there any way to disable this elsewhere for all models?
但是,我宁愿不更改 Laravel 的核心文件,也不想让我的每个模型都将其放在顶部。有没有办法在其他地方为所有型号禁用此功能?
回答by bgallagh3r
You either have to declare public $timestamps = false;
in every model, or create a BaseModel, define it there, and have all your models extend it instead of eloquent. Just bare in mind pivot tables MUST have timestamps if you're using Eloquent.
您要么必须public $timestamps = false;
在每个模型中声明,要么创建一个 BaseModel,在那里定义它,然后让所有模型扩展它而不是雄辩。如果您使用 Eloquent,请记住数据透视表必须有时间戳。
Update: Note that timestamps are no longer REQUIRED in pivot tables after Laravel v3.
更新:请注意,在 Laravel v3 之后,数据透视表中不再需要时间戳。
Update: You can also disable timestamps by removing $table->timestamps()
from your migration.
更新:您还可以通过$table->timestamps()
从迁移中删除来禁用时间戳。
回答by PhilMarc
Simply place this line in your Model:
只需将此行放在您的模型中:
public $timestamps = false;
public $timestamps = false;
And that's it!
就是这样!
Example:
例子:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public $timestamps = false;
//
}
To disable timestamps for one operation(e.g. in a controller):
要为一项操作(例如在控制器中)禁用时间戳:
$post->content = 'Your content';
$post->timestamps = false; // Will not modify the timestamps on save
$post->save();
To disable timestamps for all of your Models, create a new BaseModel
file:
要为所有 Models禁用时间戳,请创建一个新BaseModel
文件:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model
{
public $timestamps = false;
//
}
Then extend each one of your Models with the BaseModel
, like so:
然后使用 扩展每个模型BaseModel
,如下所示:
<?php
namespace App;
class Post extends BaseModel
{
//
}
回答by Sampath Perera
If you only need to only to disable updating updated_at just add this method to your model.
如果您只需要禁用更新 updated_at 只需将此方法添加到您的模型中。
public function setUpdatedAtAttribute($value)
{
// to Disable updated_at
}
This will override the parent setUpdatedAtAttribute() method. created_at will work as usual. Same way you can write a method to disable updating created_at only.
这将覆盖父 setUpdatedAtAttribute() 方法。created_at 将照常工作。同样,您可以编写一个方法来禁用仅更新 created_at。
回答by Sampath Perera
If you are using 5.5.x:
如果您使用的是 5.5.x:
const UPDATED_AT = null;
And for 'created_at' field, you can use:
对于“created_at”字段,您可以使用:
const CREATED_AT = null;
Make sure you are on the newest version. (This was broken in Laravel 5.5.0 and fixed again in 5.5.5).
确保您使用的是最新版本。(这在 Laravel 5.5.0 中被破坏并在 5.5.5 中再次修复)。
回答by srmilon
Eloquent Model:
雄辩模型:
class User extends Model
{
protected $table = 'users';
public $timestamps = false;
}
Or Simply try this
或者干脆试试这个
$users = new Users();
$users->timestamps = false;
$users->name = 'John Doe';
$users->email = '[email protected]';
$users->save();
回答by realplay
In case you want to remove timestamps from existing model, as mentioned before, place this in your Model:
如果您想从现有模型中删除时间戳,如前所述,请将其放在您的模型中:
public $timestamps = false;
Also create a migration with following code in the up()
method and run it:
还要在up()
方法中使用以下代码创建迁移并运行它:
Schema::table('your_model_table', function (Blueprint $table) {
$table->dropTimestamps();
});
You can use $table->timestamps()
in your down()
method to allow rolling back.
您可以$table->timestamps()
在您的down()
方法中使用以允许回滚。
回答by Shahrukh Anwar
just declare the public
timestamps variable in your Model
to false
and everything will work great.
只需public
在您的Model
to 中声明时间戳变量false
,一切都会很好。
public $timestamps = false;
public $timestamps = false;
回答by Jignesh Joisar
Add this line into your model:
将此行添加到您的模型中:
Overwrite existing variable
$timestamps
trueto false
覆盖现有变量
$timestamps
真到假
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
回答by Snapey
You can temporarily disable timestamps
您可以暂时禁用时间戳
$timestamps = $user->timestamps;
$user->timestamps=false; // avoid view updating the timestamp
$user->last_logged_in_at = now();
$user->save();
$user->timestamps=$timestamps; // restore timestamps
回答by Krishan
Override the functions setUpdatedAt()
and getUpdatedAtColumn()
in your model
覆盖模型中的函数setUpdatedAt()
和getUpdatedAtColumn()
public function setUpdatedAt($value)
{
//Do-nothing
}
public function getUpdatedAtColumn()
{
//Do-nothing
}