Standardize Data Formats in Your Laravel Application

Packages

November 9th, 2021

Standardize Data Formats in Your Laravel Application

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:

use MichaelRubel\Formatters\Collection\DateFormatter;
use MichaelRubel\Formatters\Collection\DateTimeFormatter;
use MichaelRubel\Formatters\Collection\LocaleNumberFormatter;
use MichaelRubel\Formatters\Collection\TableColumnFormatter;
 
 
format(DateFormatter::class, now())
// "2021-11-09"
 
format(DateTimeFormatter::class, now())
// "2021-11-09 02:28"
 
format(LocaleNumberFormatter::class, 1)
// "1.00"
 
config()->set('app.locale', 'es');
format(LocaleNumberFormatter::class, 1);
// "1,00"
 
format(TableColumnFormatter::class, 'created_at')
// "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:

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

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.