Let's say, I have some Model with updated_at attribute. I want to show it in template like this
{{ $model->updated_at }}
This can be used in many places so I DO NOT do this
{{ $model->updated_at->format('d/m/Y H:i:s') }}
But instead
public function getUpdatedAtAttribute($value)
{
return $value->format('d/m/Y H:i:s');
}
But there is one big problem with this solution, I can not use $model->updated_at in controllers anymore, because it returns string, not Carbon instance.
Two possible solutions
1.
public function getUpdatedAtFormatterAttribute($value)
{
return $this->updated_at->format('d/m/Y H:i:s');
}
and in template
{{ $model->updated_at_formatted }}
2.
Use package like this
class ModelPresenter extends Presenter {
public function updated_at()
{
return $this->updated_at->format('d/m/Y H:i:s');
}
}
And
{{ $model->present()->updated_at }}
From architectural point view I think second version is cleaner but are there some proven solutions? This can be not about Laravel only but about any popular PHP framework.