我正在使用 Laravel 5.1
几天前,我protected $dates = ['license_expire']
在我的模型中使用将字符串日期转换为 Carbon 实例。在 HTML 中,日期的创建表单中的默认值是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
当我用它检查日期时,dd($employee->license_expire)
它再次变为 STRING。
谁能告诉我在这种情况下如何将字符串转换为碳?
或者
将创建表单的默认日期设置为今天的日期,从数据库中编辑表单的日期,我可以使用 diffForHumans() 在主页中显示警报?