You could use an accessor :-)
Best way to repeatly format a date in my language (italian)
Hello, I am new to Laravel and OOP in general, trying to switch from old procedural code. I'd like to show some dates coming from my Models in my language (italian) for readability reasons: so far, that's what I did:
- change 'locale' => 'it' in config/app.php file
- put setlocale(LC_TIME, config('app.locale')); in routes.php file, see also my previous question
- put protected $dates = ['published_at', 'expires_at']; in my Model(s) to leverage Carbon
- put {{ utf8_encode($record->published_at->formatLocalized('%A %d %B %Y')) }} in my Blade view(s)
(NOTE: I MUST use utf8_encode(), otherwise dates with accented letters (lunedì, martedì, etc.) don't show up: don't know why and can't find the answer googling... is there a reason for this? Is it a Laravel or Carbon bug?)
My question is about the last point: writing that long {{ utf8_encode($record->published_at->formatLocalized('%A %d %B %Y')) }} piece of code every time is tedious and it would be faster (and reusable) to write {{ formatItalian($record->published_at) }}
What's the best way to accomplish this? I read about the custom helpers functions, would this be a good approach? As I said, I'm new to Laravel switching from procedural code, and trying to use the best practices, so sorry for the dumb question... Thanks
Please or to participate in this conversation.