[Laravel 扩展推荐] Laravel Formatters 标准化数据输出格式
Laravel Formatters 是由 Michael Rubel 开发的包,它提供了一组类,您可以使用它来标准化 Laravel 程序中的数据格式:
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"
如果要扩展包提供的内置格式化程序,您可以扩展服务提供者定义来覆盖它:
use MichaelRubel\Formatters\Collection\DateTimeFormatter;
$this->app->extend(DateTimeFormatter::class, function ($formatter) {
$formatter->datetime_format = 'Y.m.d H:i';
return $formatter;
});
你可以了解更多关于这个软件包的信息,获得完整的安装说明,并在 Github 上查看源代码。
这个包被提交到我们的 Laravel 新闻链接部分。链接是一个社区可以发布围绕 Laravel 生态系统软件包和教程的地方。关注 Twitter @LaravelLinks
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
推荐文章: