{"id":22406,"date":"2024-11-21T14:58:16","date_gmt":"2024-11-21T06:58:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22406\/"},"modified":"2024-11-21T14:58:16","modified_gmt":"2024-11-21T06:58:16","slug":"laravel%e6%80%8e%e4%b9%88%e5%8f%96%e6%b6%88created_at","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22406\/","title":{"rendered":"laravel\u600e\u4e48\u53d6\u6d88created_at"},"content":{"rendered":"<p>\u5728laravel\u6846\u67b6\u4e2d\uff0c\u6bcf\u4e2aeloquent\u6a21\u578b\u90fd\u9ed8\u8ba4\u5305\u542b\u4e86created_at\u548cupdated_at\u4e24\u4e2a\u65f6\u95f4\u6233\u5b57\u6bb5\u3002\u8fd9\u4e24\u4e2a\u5b57\u6bb5\u8bb0\u5f55\u4e86\u6a21\u578b\u7684\u521b\u5efa\u548c\u66f4\u65b0\u65f6\u95f4\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u65b9\u4fbf\u8bb0\u5f55\u6570\u636e\u7684\u66f4\u65b0\u65f6\u95f4\uff0c\u4ee5\u53ca\u65b9\u4fbf\u5b9e\u73b0\u4e00\u4e9b\u529f\u80fd\uff0c\u6bd4\u5982\u6309\u7167\u521b\u5efa\u65f6\u95f4\u548c\u66f4\u65b0\u65f6\u95f4\u6392\u5e8f\u7b49\u7b49\u3002<\/p>\n<p>\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u9700\u8981\u8fd9\u4e24\u4e2a\u65f6\u95f4\u6233\u5b57\u6bb5\u3002\u6bd4\u5982\u6709\u4e9b\u8001\u7684\u6570\u636e\u5e93\u8868\u53ef\u80fd\u6ca1\u6709\u8fd9\u4e24\u4e2a\u5b57\u6bb5\uff0c\u6216\u8005\u6211\u4eec\u6839\u672c\u4e0d\u9700\u8981\u8bb0\u5f55\u6570\u636e\u7684\u521b\u5efa\u548c\u66f4\u65b0\u65f6\u95f4\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u6a21\u578b\u4e2d\u52a0\u5165\u4e0b\u9762\u8fd9\u4e24\u884c\u4ee3\u7801\u6765\u53d6\u6d88 created_at \u548c updated_at\uff1a<\/p>\n<pre>public $timestamps = false;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6837\u505a\u53ef\u4ee5\u5728\u6a21\u578b\u4e2d\u53d6\u6d88 created_at \u548c updated_at \u5b57\u6bb5\u3002\u4f46\u662f\u8981\u6ce8\u610f\u7684\u662f\uff0c\u53d6\u6d88\u8fd9\u4e24\u4e2a\u65f6\u95f4\u6233\u5b57\u6bb5\u4f1a\u5bfc\u81f4\u4e00\u4e9bLaravel\u5185\u7f6e\u7684\u529f\u80fd\u5931\u6548\u3002\u6bd4\u5982\u6a21\u578b\u65e0\u6cd5\u901a\u8fc7 created_at \u548c updated_at \u5b57\u6bb5\u8fdb\u884c\u6392\u5e8f\uff0c\u4e5f\u65e0\u6cd5\u81ea\u52a8\u8bb0\u5f55\u6570\u636e\u7684\u521b\u5efa\u548c\u66f4\u65b0\u65f6\u95f4\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u53ea\u9700\u8981\u53d6\u6d88\u5176\u4e2d\u4e00\u4e2a\u65f6\u95f4\u6233\u5b57\u6bb5\uff0c\u6bd4\u5982\u53ea\u53d6\u6d88 created_at\uff0c\u90a3\u4e48\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre>\/\/ \u53ea\u53d6\u6d88 created_at\nconst UPDATED_AT = 'updated_at';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u662f\u5c06 UPDATED_AT \u5e38\u91cf\u8d4b\u503c\u4e3a updated_at \u5b57\u7b26\u4e32\uff0c\u800c\u9ed8\u8ba4\u60c5\u51b5\u4e0b UPDATED_AT \u7684\u503c\u662f updated_at\uff0c\u56e0\u6b64\u8fd9\u6837\u505a\u53ea\u53d6\u6d88\u4e86 created_at\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u53d6\u6d88\u4e86\u8fd9\u4e9b\u65f6\u95f4\u6233\u5b57\u6bb5\u540e\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u5b9e\u73b0\u81ea\u5df1\u7684\u6570\u636e\u521b\u5efa\u65f6\u95f4\u548c\u66f4\u65b0\u65f6\u95f4\u7684\u8bb0\u5f55\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u9700\u8981\u624b\u52a8\u7f16\u5199\u4ee3\u7801\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>\/\/ \u5728\u6a21\u578b\u4e2d\u5b9e\u73b0\u81ea\u5b9a\u4e49\u65f6\u95f4\u6233\u5b57\u6bb5\nprotected $dateFormat = 'U';\nprotected static function boot()\n{\n    parent::boot();\n    static::creating(function ($model) {\n        $model-&gt;create_time = $model-&gt;freshTimestamp();\n    });\n    static::updating(function ($model) {\n        $model-&gt;update_time = $model-&gt;freshTimestamp();\n    });\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u6a21\u578b\u4e2d\u5b9a\u4e49\u4e86 create_time \u548c update_time \u4e24\u4e2a\u5b57\u6bb5\u6765\u8bb0\u5f55\u6570\u636e\u7684\u521b\u5efa\u65f6\u95f4\u548c\u66f4\u65b0\u65f6\u95f4\u3002\u540c\u65f6\uff0c\u6211\u4eec\u901a\u8fc7\u5b9a\u4e49 $dateFormat \u5c5e\u6027\u6765\u4fee\u6539\u9ed8\u8ba4\u7684\u65f6\u95f4\u683c\u5f0f\u3002\u5728 boot \u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7 creating \u548c updating \u4e8b\u4ef6\u6765\u4e3a create_time \u548c update_time \u8d4b\u503c\u3002freshTimestamp \u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6700\u65b0\u7684\u65f6\u95f4\u6233\u3002<\/p>\n<p>\u603b\u7ed3\u4e00\u4e0b\uff0c\u53d6\u6d88 created_at \u548c updated_at \u53ef\u4ee5\u901a\u8fc7\u5728\u6a21\u578b\u4e2d\u6dfb\u52a0 $timestamps = false \u6216\u4fee\u6539 UPDATED_AT \u5e38\u91cf\u6765\u5b9e\u73b0\u3002\u53d6\u6d88\u8fd9\u4e9b\u65f6\u95f4\u6233\u5b57\u6bb5\u4f1a\u5f71\u54cdLaravel\u4e2d\u4e00\u4e9b\u5185\u7f6e\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u6392\u5e8f\u548c\u81ea\u52a8\u8bb0\u5f55\u65f6\u95f4\u7b49\u7b49\u3002\u5982\u679c\u9700\u8981\u81ea\u5df1\u5b9e\u73b0\u65f6\u95f4\u6233\u7684\u8bb0\u5f55\uff0c\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u7f16\u5199\u4ee3\u7801\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u600e\u4e48\u53d6\u6d88created_at\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728laravel\u6846\u67b6\u4e2d\uff0c\u6bcf\u4e2aeloquent\u6a21\u578b\u90fd\u9ed8\u8ba4\u5305\u542b\u4e86created_at\u548cupdated_at\u4e24\u4e2a\u65f6\u95f4\u6233\u5b57\u6bb5\u3002\u8fd9\u4e24\u4e2a\u5b57\u6bb5\u8bb0\u5f55\u4e86\u6a21\u578b\u7684\u521b\u5efa\u548c\u66f4\u65b0\u65f6\u95f4\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u65b9\u4fbf\u8bb0\u5f55\u6570\u636e\u7684\u66f4\u65b0\u65f6\u95f4\uff0c\u4ee5\u53ca\u65b9\u4fbf\u5b9e\u73b0\u4e00\u4e9b\u529f\u80fd\uff0c\u6bd4\u5982\u6309\u7167\u521b\u5efa\u65f6\u95f4\u548c\u66f4\u65b0\u65f6\u95f4\u6392\u5e8f\u7b49\u7b49\u3002 \u7136\u800c\uff0c\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u9700\u8981\u8fd9\u4e24\u4e2a\u65f6\u95f4\u6233\u5b57\u6bb5\u3002\u6bd4\u5982\u6709\u4e9b\u8001\u7684\u6570\u636e\u5e93\u8868\u53ef\u80fd\u6ca1\u6709\u8fd9\u4e24\u4e2a\u5b57\u6bb5\uff0c\u6216\u8005\u6211\u4eec\u6839\u672c\u4e0d\u9700\u8981\u8bb0\u5f55\u6570\u636e\u7684\u521b\u5efa\u548c\u66f4\u65b0\u65f6\u95f4\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u6a21\u578b\u4e2d\u52a0\u5165\u4e0b\u9762\u8fd9\u4e24\u884c\u4ee3\u7801\u6765\u53d6\u6d88 created_at \u548c updated_at\uff1a public $timestamps = false; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6837\u505a\u53ef\u4ee5\u5728\u6a21\u578b\u4e2d\u53d6\u6d88 created_at \u548c updated_at \u5b57\u6bb5\u3002\u4f46\u662f\u8981\u6ce8\u610f\u7684\u662f\uff0c\u53d6\u6d88\u8fd9\u4e24\u4e2a\u65f6\u95f4\u6233\u5b57\u6bb5\u4f1a\u5bfc\u81f4\u4e00\u4e9bLaravel\u5185\u7f6e\u7684\u529f\u80fd\u5931\u6548\u3002\u6bd4\u5982\u6a21\u578b\u65e0\u6cd5\u901a\u8fc7 created_at \u548c updated_at \u5b57\u6bb5\u8fdb\u884c\u6392\u5e8f\uff0c\u4e5f\u65e0\u6cd5\u81ea\u52a8\u8bb0\u5f55\u6570\u636e\u7684\u521b\u5efa\u548c\u66f4\u65b0\u65f6\u95f4\u3002 \u5982\u679c\u6211\u4eec\u53ea\u9700\u8981\u53d6\u6d88\u5176\u4e2d\u4e00\u4e2a\u65f6\u95f4\u6233\u5b57\u6bb5\uff0c\u6bd4\u5982\u53ea\u53d6\u6d88 created_at\uff0c\u90a3\u4e48\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a \/\/ \u53ea\u53d6\u6d88 created_at const UPDATED_AT = &#8216;updated_at&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4e2a\u65b9\u6cd5\u662f\u5c06 UPDATED_AT \u5e38\u91cf\u8d4b\u503c\u4e3a updated_at \u5b57\u7b26\u4e32\uff0c\u800c\u9ed8\u8ba4\u60c5\u51b5\u4e0b UPDATED_AT \u7684\u503c\u662f updated_at\uff0c\u56e0\u6b64\u8fd9\u6837\u505a\u53ea\u53d6\u6d88\u4e86 created_at\u3002 \u5f53\u6211\u4eec\u53d6\u6d88\u4e86\u8fd9\u4e9b\u65f6\u95f4\u6233\u5b57\u6bb5\u540e\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u5b9e\u73b0\u81ea\u5df1\u7684\u6570\u636e\u521b\u5efa\u65f6\u95f4\u548c\u66f4\u65b0\u65f6\u95f4\u7684\u8bb0\u5f55\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u9700\u8981\u624b\u52a8\u7f16\u5199\u4ee3\u7801\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a \/\/ \u5728\u6a21\u578b\u4e2d\u5b9e\u73b0\u81ea\u5b9a\u4e49\u65f6\u95f4\u6233\u5b57\u6bb5 protected $dateFormat = &#8216;U&#8217;; protected static function boot() { parent::boot(); static::creating(function ($model) { $model-&gt;create_time [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22406","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22406"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22406\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}