Standardize Data Formats in Your Laravel Application

Packages

November 9th, 2021

laravel-formatters-featured.png

Laravel Formatters is a package by Michael Rubel that provides a collection of classes you can use to standardize data formats in your Laravel application:

1use MichaelRubel\Formatters\Collection\DateFormatter;
2use MichaelRubel\Formatters\Collection\DateTimeFormatter;
3use MichaelRubel\Formatters\Collection\LocaleNumberFormatter;
4use MichaelRubel\Formatters\Collection\TableColumnFormatter;
5 
6 
7format(DateFormatter::class, now())
8// "2021-11-09"
9 
10format(DateTimeFormatter::class, now())
11// "2021-11-09 02:28"
12 
13format(LocaleNumberFormatter::class, 1)
14// "1.00"
15 
16config()->set('app.locale', 'es');
17format(LocaleNumberFormatter::class, 1);
18// "1,00"
19 
20format(TableColumnFormatter::class, 'created_at')
21// "Created at"

If you want to extend a built-in formatter provided by this package, you can extend the service provider definition to override it:

1use MichaelRubel\Formatters\Collection\DateTimeFormatter;
2 
3$this->app->extend(DateTimeFormatter::class, function ($formatter) {
4 $formatter->datetime_format = 'Y.m.d H:i';
5 
6 return $formatter;
7});

You can learn more about this package, get full installation instructions, and view the source code on GitHub.


This package was submitted to our Laravel News Links section. Links is a place the community can post packages and tutorials around the Laravel ecosystem. Follow along on Twitter @LaravelLinks

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.