I need to format a string passed from controller once it's in the view while a foreach loop runs. Example:
Controller:
class MyController {
public function index()
{
$model = Events::all();
return View::make('foo.index')->with('model', $model);
}
View:
@foreach($model as $item)
<div>{{$model->timestamp}}</div>
@endforeach`
Here's the tricky part; I want to run a few formatting options before echoing it out. For instance, I need to echo out the months from the timestamp in french. Since I'm trying to avoid having any code in the view, where do I put this and how do I call it?
Example formatting:
$month = date_parse($model->timestamp);
switch ($month['month']) {
case '1':
$month = 'Janvier';
break;
case '2':
$month = 'Février';
break;
...
}
I know this would work, but is obviously bad code practice:
@foreach($model as $item)
$month = date_parse($item->timestamp);
switch ($month['month']) {
case '1':
$month = 'Janvier';
break;
case '2':
$month = 'Février';
break;
...
}
<div>{{ $month }}</div>
@endforeach`
Any help or tips would be appreciated! Note: I know usually you would take care of formatting in the controller, but since I'm running I can't "pre-format" the output until I run the foreach loop, I'm a bit at a loss here about best convention.
Thanks!