[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 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://laravel-news.com/standardize-dat...

译文地址:https://learnku.com/laravel/t/62851

本文为协同翻译文章,如您发现瑕疵请点击「改进」按钮提交优化建议
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!