2017 © Pedro Peláez
 

package laravel-datetime-mutator

Laravel traits mutators to help Date Time manipulation on Eloquent Models

image

torzer/laravel-datetime-mutator

Laravel traits mutators to help Date Time manipulation on Eloquent Models

  • Saturday, September 23, 2017
  • by nunomazer
  • Repository
  • 2 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 4 Versions
  • 60 % Grown

The README.md

laravel-datetime-mutator

Laravel traits mutators to help Date Time manipulation on Eloquent Models., (*1)

Installing

composer require torzer/laravel-datetime-mutator

Traits

MapDateTimeMutator

The MapDateTimeMutator trait is used to set a date and time mutator (only date or time if necessary) related to a specific input format., (*2)

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., (*3)

To use it, set in the class:, (*4)

<?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:, (*5)

<?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., (*6)

The approved_at field in $dates array is still handled with the default behavior of the framework., (*7)

Note that the input value of this fields - start_date and finish_date, must be in d/m/Y format., (*8)

Using Jenssegers/MongoDB

If you are using Jenssegers/MongoDB driver to persist date only, this trait can help you to handle MongoDB timestamp field type., (*9)

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:, (*10)

<?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'
    ];

    ...


The Versions

23/09 2017

dev-master

9999999-dev

Laravel traits mutators to help Date Time manipulation on Eloquent Models

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Ademir Mazer Junior
by torzer
by Oseas Tormen
by Oseas Tormen

03/08 2017

1.1.1

1.1.1.0

Laravel traits mutators to help Date Time manipulation on Eloquent Models

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Ademir Mazer Junior
by torzer
by Oseas Tormen

02/08 2017

1.1.0

1.1.0.0

Laravel traits mutators to help Date Time manipulation on Eloquent Models

  Sources   Download

MIT

by Ademir Mazer Junior
by torzer
by Oseas Tormen

01/08 2017

1.0.1

1.0.1.0

Laravel traits mutators to help Date Time manipulation on Eloquent Models

  Sources   Download

MIT

by Ademir Mazer Junior
by torzer
by Oseas Tormen