禁用 Laravel 的 Eloquent 时间戳

2023-03-04php开发问题
0

本文介绍了禁用 Laravel 的 Eloquent 时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在将我们的一个 Web 应用程序从 CodeIgniter 转换为 Laravel.然而,此时我们不想将 updated_at/created_at 字段添加到我们所有的表中,因为我们有一个日志类可以更深入地完成所有这些工作已经.

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; ,要么创建一个 BaseModel,在那里定义它,并有你所有的模型都扩展了它而不是雄辩.如果您使用 Eloquent,请记住数据透视表必须有时间戳.

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 之后,数据透视表中不再需要时间戳.

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.

这篇关于禁用 Laravel 的 Eloquent 时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17