Download the PHP package emilienkopp/formats-dates-trait without Composer
On this page you can find all versions of the php package emilienkopp/formats-dates-trait. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download emilienkopp/formats-dates-trait
More information about emilienkopp/formats-dates-trait
Files in emilienkopp/formats-dates-trait
Package formats-dates-trait
Short Description A Laravel-Carbon trait for easy date formatting
License MIT
Informations about the package formats-dates-trait
Laravel Dates Formatter Trait
A Laravel trait that provides elegant date formatting capabilities to your models with automatic format detection and magic method support.
Features
- 🔄 Automatic format detection based on property names and types
- ✨ Magic method support for cleaner syntax
- 🔍 Reflection-based property access
- 🛡️ Null-safe operations
- 🎨 Customizable date formats
- 🎯 Type-hint aware
Installation
You can install the package via composer:
Usage
Add the trait to your model:
Now you can use 'getFormattedDate' method or magic methods to format your dates.
⚠️ Note: magic methods are camelCase
versions of the property names.
Automatic Format Detection
The trait automatically determines the appropriate format based on property names and types. It uses strict suffix matching to avoid false positives.
Format Detection Rules (in order of precedence)
-
Type hints
- Properties typed as
DateTime
→ Uses datetime format
- Properties typed as
- Property name suffixes
- Ends with
_datetime
or_at
→ Uses datetime format - Ends with
_time
→ Uses time format - Ends with
_date
→ Uses date format - All others → Uses default date format
- Ends with
Magic methods are generated based on the suffixes.
Therefore, any property that does not follow the suffix rules will not have a magic method invoked
and will likely throw a BadMethodCallException
.
Examples
Basic Usage
Customizing Default Formats
You can customize the default formats for your model:
⚠️ Note: Format changes are applied per model class, not globally.
Null Safety
The trait handles null values gracefully:
License
The MIT License (MIT). Please see License File for more information.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security
If you discover any security related issues, please contact me instead of using the issue tracker.
Support Laravel Versions
- Laravel 7.x through 11.x
- PHP 7.4 or higher
All versions of formats-dates-trait with dependencies
nesbot/carbon Version ^2.0|^3.0
illuminate/support Version ^7.0|^8.0|^9.0|^10.0|^11.0