我知道如何使用Date和DateTime.但在这种情况下,我希望我的数据是2023-03-03星期五.Laravel 提供这样的东西吗?

这是我的代码

$table->date('estimate_time_arrival');

我怎么修改它呢?

推荐答案

我的建议是以默认格式(Y-m-d)存储日期.使用拉威尔投射功能.

方法1:Defining An Accessor

 protected function estimateTimeArrival(): Attribute
    {
        return Attribute::make(
            get: fn (string $value) => Carbon::parse($value)->format('Y-m-d l'),
        );
    }

如果打印$record->estimate_time_arrival$record->toArray(),则输出如下

enter image description here

方法2:在你的模型中有Attribute Casting

 protected $casts=[
        'estimate_time_arrival' => 'datetime:Y-m-d l',
    ];

如果打印$record->estimate_time_arrival$record->toArray(),则输出如下

enter image description here

如果您看到输出,那么如果我们try 直接访问,那么它将返回Carbon Object,而格式化输出仅在您使用toArray()时返回

方法三:

 protected $casts=[
        'estimate_time_arrival' => 'datetime:Y-m-d l',
    ];

如果打印$record->estimate_time_arrival->format('Y-m-d l')$record->toArray(),则输出如下

enter image description here

Php相关问答推荐

Laravel—通过单击处理多条路由

从WooCommerce Checkout Country Select2下拉列表中重新排序国家/地区

保存PHP条带以备将来使用,不显示用户界面

无法在WooCommerce中发送客户笔记邮箱通知

如何在函数中定位WooCommerce产品循环

如何将WooCommerce产品属性术语名称显示为链接术语?

由于某种原因,FILE_GET_CONTENTS从一天到另一天停止工作,并在PHP 7.2.34中返回一条&http/1.1 406不能接受&的错误

WooCommerce-在选定类别中每消费X个金额即可添加免费礼物

使用与OpenSSL命令行兼容的AES-256-CTR为大型文件创建php代码OpenSSL加密

shell_exec运行大型数据处理

$this->;db->;update();CodIgnitor中的功能不工作

将自定义保存金额移至 Woocommerce 简单产品中的价格以下

循环中未序列化数组显示的问题

如何在 php 中获取正确的时间

WooCommerce checkout 流程中基于所选选项添加自定义费用

在这个 Laravel 应用中,如何仅显示已批准的 comments 回复?

WooCommerce - 调用功能ID不正确.不应直接访问订单属性

计算组合指数

Laravel 模型将日期保存为空

fopen 功能不断向我发送我重新加载网页时已发布的重复版本的表单