Package Data | |
---|---|
Maintainer Username: | nunomazer |
Maintainer Contact: | torzer@torzer.com (torzer) |
Package Create Date: | 2017-08-01 |
Package Last Update: | 2017-09-23 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-23 03:05:13 |
Package Statistics | |
---|---|
Total Downloads: | 352 |
Monthly Downloads: | 6 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 3 |
Total Forks: | 2 |
Total Open Issues: | 1 |
Laravel traits mutators to help Date Time manipulation on Eloquent Models.
composer require torzer/laravel-datetime-mutator
The MapDateTimeMutator
trait is used to set a date and time mutator (only date or time if necessary)
related to a specific input format.
WHen using this trait it's not necessary to implement a setVariableAttribute($value) mutator to transform a date from a format to another, WHen using this trait it's not necessary to implement a setVariableAttribute($value) mutator to transform a date from a format to another, as for example from d/m/Y format of a string or object to another that will be used to persist the date in database.
To use it, set in the class:
<?php
use Torzer\Common\Traits\MapDateTimeMutator;
class MyClass extends Model {
use MapDateTimeMutator;
..
Set the date fields as you would do in array $dates, but to those dates or timestamps that need to be transformed
from one format to another, use the array $mapDateTimeMutator
with the
name of the date field as the array key and an array mapping from
and to
formats:
<?php
use Torzer\Common\Traits\MapDateTimeMutator;
class MyClass extends Model {
use MapDateTimeMutator;
protected $mapDateTimeMutator = [
'start_date' => ['from' => 'd/m/Y', 'to' => 'Y-m-d'],
'finish_date' => ['from' => 'd/m/Y', 'to' => 'Y-m-d']
];
protected $dates = [
'approved_at', 'start_date', 'finish_date'
];
...
At the example above, the fields start_date
and finish_date
gonna be handle with the DateTime function of Laravel Eloquent Model,
but they arecreated from format d/m/Y
set in from
key of the $mapDateTimeMutator
array,
getting as return/setAttribute of the field a string formated using the to
key.
The approved_at
field in $dates
array is still handled with the default behavior of the framework.
Note that the input value of this fields - start_date
and finish_date
, must be in d/m/Y
format.
If you are using Jenssegers/MongoDB driver to persist date only, this trait can help you to handle MongoDB timestamp field type.
To ensure a date gonna be saved in UTC timezone at hour 00:00:00, you must add a date-only
setting in array $mapDateTimeMutator
:
<?php
use Torzer\Common\Traits\MapDateTimeMutator;
class MyClass extends Jenssegers\Mongodb\Eloquent\Model {
use MapDateTimeMutator;
protected $mapDateTimeMutator = [
'start_date' => ['from' => 'd/m/Y', 'to' => 'Y-m-d'],
'finish_date' => ['from' => 'd/m/Y', 'to' => 'Y-m-d'],
'date-only' => true,
];
protected $dates = [
'approved_at', 'start_date', 'finish_date'
];
...