将字符串转换为碳
我使用的是 Laravel 5.1
几天前,我在模型中使用字符串日期转换为 Carbon 实例。在 HTML 中,日期的创建表单中的默认值为protected $dates = ['license_expire']
Carbon\Carbon::now()->format('Y-m-d')
为了在主页中显示警报,我使用了<p>Licence Expired: <b>{{ $employee->license_expire < Carbon\Carbon::now()?'License has expired':$employee->license_expire->diffForHumans() }}</b></p>
在此之前,diffForHumans() 方法工作正常。
但是在这种情况下,编辑表单的默认值也是今天的日期,无论数据库中有什么(我使用的是部分表单)。为了解决这个问题,我将HTML中的默认值更改为NUll。并在我的模型中添加另一种方法,以创建形式显示当前日期。
public function getLicenseExpireAttribute($date)
{
return Carbon::parse($date)->format('Y-m-d');
}
之后,当我去主页时,我有一个说FatalErrorException
Call to a member function diffForHumans() on string
当我检查日期时,它再次成为STRING。dd($employee->license_expire)
任何人都可以告诉我在这种情况下如何将字符串转换为Carbon?
或
使我的创建表单的默认日期作为今天的日期,编辑表单从数据库的日期,我可以使用diffForHumans()在主页中显示警报?