如何获取日期并将其插入到使用Laravel的php中[关闭]

如何获取日期并将其插入到使用Laravel的php中[关闭]

问题描述:

I want to know how can I make Laravel to get the current date and insert it into the database with the rest of the data from a form.

Thanks in advance.

我想知道如何让Laravel获取当前日期并将其插入到数据库中 表格中的数据。 p>

提前致谢。 p> div>

Laravel comes with this functionality out of the box.

You can add this code to your migration:

$table->timestamps();

This will create a created_at and updated_at field in your database which are automatically filled.


Or if you want to don't want to do it within Laravel, you can do one of these:

  • Set the default value of the column to: TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
  • Assign the value in the controller: $model->created_at = date('Y-m-d'); and save it normally.

Laravel has the ability to save create and update timestamps in the database automatically.

All you need to do is add $table->timestamps() to your migration. This will create two new fields: created_at and updated_at. Laravel will automatically populate those fields when creating/updating a model.

For automatically insertion you can use

$table->timestamps();

And for static entry you can use

 $table->Date('Date_Of_Birth');