<legend id='o79Kg'><style id='o79Kg'><dir id='o79Kg'><q id='o79Kg'></q></dir></style></legend>
  • <i id='o79Kg'><tr id='o79Kg'><dt id='o79Kg'><q id='o79Kg'><span id='o79Kg'><b id='o79Kg'><form id='o79Kg'><ins id='o79Kg'></ins><ul id='o79Kg'></ul><sub id='o79Kg'></sub></form><legend id='o79Kg'></legend><bdo id='o79Kg'><pre id='o79Kg'><center id='o79Kg'></center></pre></bdo></b><th id='o79Kg'></th></span></q></dt></tr></i><div id='o79Kg'><tfoot id='o79Kg'></tfoot><dl id='o79Kg'><fieldset id='o79Kg'></fieldset></dl></div>

      • <bdo id='o79Kg'></bdo><ul id='o79Kg'></ul>
      <tfoot id='o79Kg'></tfoot>

    1. <small id='o79Kg'></small><noframes id='o79Kg'>

      1. Laravel 在更新时更改 created_at

        时间:2023-09-23
      2. <small id='e38zy'></small><noframes id='e38zy'>

                <bdo id='e38zy'></bdo><ul id='e38zy'></ul>

                1. <legend id='e38zy'><style id='e38zy'><dir id='e38zy'><q id='e38zy'></q></dir></style></legend>
                    <tbody id='e38zy'></tbody>

                2. <i id='e38zy'><tr id='e38zy'><dt id='e38zy'><q id='e38zy'><span id='e38zy'><b id='e38zy'><form id='e38zy'><ins id='e38zy'></ins><ul id='e38zy'></ul><sub id='e38zy'></sub></form><legend id='e38zy'></legend><bdo id='e38zy'><pre id='e38zy'><center id='e38zy'></center></pre></bdo></b><th id='e38zy'></th></span></q></dt></tr></i><div id='e38zy'><tfoot id='e38zy'></tfoot><dl id='e38zy'><fieldset id='e38zy'></fieldset></dl></div>
                3. <tfoot id='e38zy'></tfoot>

                  本文介绍了Laravel 在更新时更改 created_at的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在这个主题上找到了这个答案,但它没有不适合我.

                  I found this answer on the subject, but it doesn't work for me.

                  所以,我在数据库中创建一个条目:

                  So, I make an entry in the database:

                  // Write lead to database
                  $lead = Lead::create($lead_data);
                  

                  时间戳看起来像这样,这很好:

                  And the timestamps look like this, which is good:

                  | 2016-01-08 10:34:15 | 2016-01-08 10:34:15 |
                  

                  然后我向外部服务器发出请求,我需要更新该行:

                  But then I make a request to an external server, and I need to update the row:

                  $lead->user_id = $response['user_id'];
                  $lead->broker_id = $response['broker_id'];
                  $lead->save();
                  

                  并且 created_at 字段被更改:

                  and the created_at field gets changed:

                  | 2016-01-08 04:34:17 | 2016-01-08 10:34:17 |
                  

                  我该如何解决这个问题?

                  How do I solve this problem?

                  编辑

                  我需要一个解决方案,它只修改行为而不删除列或重置迁移.必须在不接触数据的情况下在实时数据库上执行修复.如下所示,我尝试了以下迁移:

                  I need a solution that would just modify the behavior without dropping columns or resetting migrations. The fix has to be performed on a live database without touching the data. As suggested below, I tried the following migration:

                  $table->datetime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'))->change();
                  

                  但没有任何反应.created_at 字段仍会在更新时修改.

                  but nothing happens. The created_at field still gets modified on update.

                  推荐答案

                  如果您使用的是 Laravel 5.2 并使用 MySQL,那么时间戳会引入一些错误".您可以在 github 此处阅读有关该问题的所有信息.它与时间戳默认值有关,MySQL 在某些条件下会自动分配 DEFAULT CURRENT_TIMESTAMP 或 ON UPDATE CURRENT_TIMESTAMP 属性.

                  If you're on Laravel 5.2 and using MySQL, there was a bit of a "bug" introduced with the timestamps. You can read all about the issue on github here. It has to do with the timestamp defaults, and MySQL automatically assigning DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes under certain conditions.

                  基本上,您有三个选择.

                  Basically, you have three options.

                  1. 更新 MySQL 变量:

                  如果您将 explicit_defaults_for_timestamp 变量设置为 TRUE,则不会自动为时间戳列分配 DEFAULT CURRENT_TIMESTAMP 或 ON UPDATE CURRENT_TIMESTAMP 属性.您可以在这里阅读更多关于变量的信息.

                  If you set the explicit_defaults_for_timestamp variable to TRUE, no timestamp column will be assigned the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes automatically. You can read more about the variable here.

                  1. 使用可为空的时间戳:

                  $table->timestamps() 更改为 $table->nullableTimestamps().默认情况下,$table->timestamps() 命令创建不可为空的时间戳字段.通过使用 $table->nullableTimestamps(),您的时间戳字段将可以为空,并且 MySQL 不会自动为第一个字段分配 DEFAULT CURRENT_TIMESTAMP 或 ON UPDATE CURRENT_TIMESTAMP 属性.

                  Change $table->timestamps() to $table->nullableTimestamps(). By default, the $table->timestamps() command creates timestamp fields that are not nullable. By using $table->nullableTimestamps(), your timestamp fields will be nullable, and MySQL will not automatically assign the first one the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes.

                  1. 自己定义时间戳:

                  不要使用 $table->timestamps,而是使用 $table->timestamp('updated_at');$table->timestamp('created_at'); 你自己.确保您的updated_at"字段是表中的第一个时间戳,以便它自动分配 DEFAULT CURRENT_TIMESTAMP 或 ON UPDATE CURRENT_TIMESTAMP 属性.

                  Instead of using $table->timestamps, use $table->timestamp('updated_at'); $table->timestamp('created_at'); yourself. Make sure your 'updated_at' field is the first timestamp in the table, so that it will be the one that is automatically assign the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes.

                  这篇关于Laravel 在更新时更改 created_at的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:laravel 搜索多个以空格分隔的单词 下一篇:有一个通过 Laravel Eloquent

                  相关文章

                  最新文章

                    <bdo id='Y587d'></bdo><ul id='Y587d'></ul>

                  <small id='Y587d'></small><noframes id='Y587d'>

                4. <legend id='Y587d'><style id='Y587d'><dir id='Y587d'><q id='Y587d'></q></dir></style></legend>

                    <tfoot id='Y587d'></tfoot>

                  1. <i id='Y587d'><tr id='Y587d'><dt id='Y587d'><q id='Y587d'><span id='Y587d'><b id='Y587d'><form id='Y587d'><ins id='Y587d'></ins><ul id='Y587d'></ul><sub id='Y587d'></sub></form><legend id='Y587d'></legend><bdo id='Y587d'><pre id='Y587d'><center id='Y587d'></center></pre></bdo></b><th id='Y587d'></th></span></q></dt></tr></i><div id='Y587d'><tfoot id='Y587d'></tfoot><dl id='Y587d'><fieldset id='Y587d'></fieldset></dl></div>