我将数据库中的数组/对象传递给视图文件,如下所示:
View::make('home.announcements')
->with('announcements', Announcements::all());
当它被传递给视图文件时,它包含一些整数,如:
$announcements[$k]->month //Output 05
我需要调用base.php
内部的函数将05转换为"May",因此我将在视图中获得"May"结果。
<span> {{ $this->convertMonthToString($announcements[$k]->month) }} </span>
//Output: <span>May</span>
我知道我可以直接通过->with
,但这不是我要问的。我不想使用额外的->with
传递额外的信息。
我希望我的视图能够处理这些基本的输出函数。
我该怎么做?
你需要在你的' announcement '模型中创建一个函数:
public function get_fullmonth()
{
return date('F', strtotime($this->get_attribute('month')));
}
在视图中调用:
@foreach ($announcements as $item)
<span>{{$item->fullmonth}}</span>
@endforeach