简体   繁体   中英

How can I reach base.php functions via my view files?

I pass an array/object from my database to view file like this:

View::make('home.announcements')
    ->with('announcements', Announcements::all());

When it is passed to the view file, it contains some integers, like:

 $announcements[$k]->month //Output 05

I need to call a function inside base.php to convert 05 to "May", so I will get "May" result in the view.

 <span> {{ $this->convertMonthToString($announcements[$k]->month) }} </span>
 //Output: <span>May</span>

I know I can pass it directly with ->with but that's not what I'm asking. I don't want to pass additional information using additional ->with 's.

I want my views to handle those basic output functions.

How can I do this?

You need to create an function in your 'Announcements' model called:

public function get_fullmonth()
{
   return date('F', strtotime($this->get_attribute('month')));
}

Call in in your view as:

@foreach ($announcements as $item)
   <span>{{$item->fullmonth}}</span>
@endforeach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM