問題描述
我正在將我們的一個 Web 應(yīng)用程序從 CodeIgniter 轉(zhuǎn)換為 Laravel.然而,此時我們不想將 updated_at
/created_at
字段添加到我們所有的表中,因為我們有一個日志類可以更深入地完成所有這些工作已經(jīng).
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.
我知道我可以在:
VendorlaravelframeworksrcilluminateDatebaseEloquentModel.php
然而,我寧愿不更改 Laravel 的核心文件,也不想讓我的每個模型都將其放在頂部.有沒有辦法在其他地方為所有模型禁用此功能?
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?
推薦答案
你要么必須在每個模型中聲明 public $timestamps = false;
,要么創(chuàng)建一個 BaseModel,在那里定義它,并有你所有的模型都擴展了它而不是雄辯.如果您使用 Eloquent,請記住數(shù)據(jù)透視表必須有時間戳.
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.
更新:請注意,在 Laravel v3 之后,數(shù)據(jù)透視表中不再需要時間戳.
Update: Note that timestamps are no longer REQUIRED in pivot tables after Laravel v3.
更新:您還可以通過從遷移中刪除 $table->timestamps()
來禁用時間戳.
Update: You can also disable timestamps by removing $table->timestamps()
from your migration.
這篇關(guān)于禁用 Laravel 的 Eloquent 時間戳的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!