【发布时间】:2015-02-25 10:05:47
【问题描述】:
我正在尝试在刀片视图中显示格式化的日期。我正在使用 Laravel 4.1。
我在config/app.php 中创建了一个别名
'Carbon' => 'Carbon\Carbon'
我按如下方式迁移我的约会表:
$table->dateTime('appointment');
查看
@foreach($appointments as $appointment)
<tr>
<td>{{{ $appointment->customer_name }}}</td>
<td>{{{ $appointment->consultant->consultant_name }}}</td>
<td>{{{ $appointment->appointment }}}</td> {{-- Displays as seen in mysql --}}
</tr>
@endforeach
如果我尝试如下格式化日期,则会引发异常:
<td>{{{ $appointment->appointment->format('Y-m-d') }}}</td>
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Call to a member function format() on a non-object
如果我将{{{ $appointment->appointment->format('Y-m-d') }}} 更改为{{{ $appointment->created_at->format('Y-m-d') }}},则日期格式正确。
如何让 laravel 和 Carbon 将 appointment 处理和格式化为可以格式化的日期?
【问题讨论】:
-
我想我可以让它工作......在我的
Appointment模型中,我覆盖getDates方法如下:public function getDates() { return array('created_at', 'updated_at', 'deleted_at', 'appointment'); } -
是的,您需要将其添加到该数组中,以便将其转换为碳对象。您应该提交自己的答案并将其标记为答案。