【问题标题】:carbon get current date when variable null even with protected $dates Laravel 5.6即使使用受保护的 $dates Laravel 5.6,当变量为空时,碳也会获取当前日期
【发布时间】:2018-10-20 02:03:36
【问题描述】:

我在 laravel 5.6 中遇到了碳问题。 从数据库返回数据时,如果时间戳字段具有空值,则 carbon 将返回当前日期,即使我有 protected $dates

db 中的published_at 为空。

型号:

protected $dates = ['published_at'];

刀片:

value="{{ old('published_at', $post->published_at)}}"

我已经在Carbon get current date if variable is null 上尝试过这个建议,但是有什么建议吗?

【问题讨论】:

    标签: php laravel laravel-blade php-carbon laravel-5.6


    【解决方案1】:

    您可以在accessors 的帮助下解决您的问题。所以,不要将published_at 添加到$dates 属性,只需定义这个访问器:

    public function getPublishedAtAttribute($published_at)
    {
        return is_null($published_at) ? null : Carbon::parse($published_at);
    }
    

    【讨论】:

    • 谢谢你这对我有用......但是......为什么是 $published_at ? "" : "" 没用?
    猜你喜欢
    • 2016-03-02
    • 1970-01-01
    • 2017-12-20
    • 2018-01-07
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2013-07-31
    相关资源
    最近更新 更多