GithubHelp home page GithubHelp logo

torzer / laravel-datetime-mutator Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 2.0 9 KB

Laravel traits mutators to help Date Time manipulation on Eloquent Models

PHP 100.00%
laravel laravel-5-package date datetime mutators traits php php-traits eloquent

laravel-datetime-mutator's Introduction

laravel-datetime-mutator

Laravel traits mutators to help Date Time manipulation on Eloquent Models.

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.

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.

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.

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

    ...

laravel-datetime-mutator's People

Contributors

nunomazer avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-datetime-mutator's Issues

Handle MongoDB date only

Based on code:

use Jenssegers\Mongodb\Eloquent\Model;

class Etapa extends Model {
    protected $dates = ['data_inicio', 'data_termino'];

    public function getDataInicioAttribute($value) {
        return \Carbon\Carbon::createFromTimestamp($value->toDateTime()->getTimestamp())->timezone('UTC');
    }

    public function getDataTerminoAttribute($value) {
        return \Carbon\Carbon::createFromTimestamp($value->toDateTime()->getTimestamp())->timezone('UTC');
    }

    public function setDataInicioAttribute($value) {
        $value = \DateTime::createFromFormat('d/m/Y', $value, new \DateTimeZone('UTC'));
        $value->setTime(0, 0, 0);

        $this->attributes['data_inicio'] = new \MongoDB\BSON\UTCDateTime($value);
    }

    public function setDataTerminoAttribute($value) {
        $value = \DateTime::createFromFormat('d/m/Y', $value, new \DateTimeZone('UTC'));
        $value->setTime(0, 0, 0);

        $this->attributes['data_termino'] = new \MongoDB\BSON\UTCDateTime($value);
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.