- How do I install laravel-formatters in a Laravel project?
- Install it with Composer using composer require michael-rubel/laravel-formatters. There are no migrations, config files, or service provider steps worth auditing, so the integration is essentially a single dependency. Check the composer.json constraints first to confirm the supported Laravel and PHP versions match your app. Pin the version explicitly rather than floating on a caret constraint.
- Which Laravel and PHP versions does laravel-formatters support?
- The exact matrix is declared in the package's composer.json, so verify it against both your current version and your next upgrade target before committing. There is no infrastructure footprint, so the only compatibility risk is the framework and language constraint. Because it is a small single-maintainer package, do not assume new Laravel majors are supported on release day.
- What kinds of data can I format with this package?
- It ships formatter classes for common presentation tasks such as money, phone numbers, names, and dates. The goal is consistent output across Blade views, API responses, and anywhere else you render derived values. You can also add your own formatter classes for formats the package does not cover.
- How does the Service Container resolution work and can I override a formatter?
- Formatters are resolved through Laravel's Service Container rather than instantiated directly, which is what makes them swappable. To change behavior, bind your own implementation for a given formatter class in a service provider and the rest of your code keeps working unchanged. This is also the cleanest way to apply project-specific conventions without forking the package.
- Can I create custom formatters and chain methods?
- Yes. The pattern is a thin class that wraps a value or model and exposes derived, display-oriented methods, so adding your own formatter is a small class plus a container binding if you want to customize an existing one. Formatters support method chaining, which keeps call sites readable when several formatting steps apply. Keep custom formatters free of side effects so they stay deterministic and testable.
- Is laravel-formatters redundant if I already use Laravel API Resources or spatie/laravel-data?
- Largely, yes for many teams. Accessors and API Resources already cover most transformation needs, and the real value of this package is convention and discoverability rather than capability. If your codebase has already standardized on API Resources, laravel-data, or view models, adding a second presentation layer creates a which-layer-formats-this ambiguity. Adopt it only if you have a measurable pain such as model bloat, format divergence between API and UI, or duplicated Blade formatting logic.
- How do I test formatters in my application?
- Because formatters are plain classes resolved from the container, you can unit test them directly by instantiating them or resolving them via app() in a test. Avoid formatters that call auth(), now(), or reach into lazy-loaded relations, since that makes assertions time- and state-dependent. Also review the package's own test suite as primary evidence of behavior, since the package has no dependents to validate it in the field.
- Can formatters cause N+1 queries or non-deterministic output?
- They can if a formatter touches lazy-loaded relations or pulls values from the current request, authenticated user, or clock. That is a code-discipline risk introduced by the pattern rather than a defect in the package. Write a short convention covering which attributes belong in a formatter versus a resource, whether relations are allowed, and eager-load anything a formatter reads. Keeping formatters pure makes them both fast and testable.
- Is laravel-formatters safe to use in production and who maintains it?
- The license is MIT with no transitive dependency weight, so Composer-level risk is minimal. The main concern is maintainer concentration: it is a single-author package with modest adoption, which means a bus factor of one. The surface is small enough to fork or vendor if it is abandoned, so treat the risk as inconvenience rather than outage, and pin the version.
- What are the alternatives to laravel-formatters?
- Laravel's native accessors and API Resources cover roughly eighty percent of use cases, and spatie/laravel-data or dedicated view models cover most of the rest. A roughly fifty-line in-house base formatter class is also a legitimate alternative if you only want the convention without the dependency. Choose this package when the appeal of a ready-made, container-resolved set of formatters outweighs maintaining that base class yourself.