2017 © Pedro Peláez
 

library j-eloquent

Convert eloquent date attributes to Jalali (Persian) dates

image

arashfarahani/j-eloquent

Convert eloquent date attributes to Jalali (Persian) dates

  • Wednesday, November 30, 2016
  • by arash.farahani
  • Repository
  • 1 Watchers
  • 1 Stars
  • 37 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 16 % Grown

The README.md

j-eloquent

Convert eloquent date attributes to Jalali (Persian) dates on the fly, With the help of convention over configuration. This package was forked from reshadman/j-eloquent in previous version, (*1)

Installation

require following line in your composer require secion :, (*2)

composer require arashfarahani/j-eloquent
"require" : {
    "arashfarahani/j-eloquent" : "1.0" 
}

Documentation

The PersianDateTrait :

By using \Diamond\JEloquent\PersianDateTrait trait in your models you can enable the plugin :, (*3)

use Diamond\JEloquent\PersianDateTrait;
use Illuminate\Database\Eloquent\Model;

class User extends Model {
    use PersianDateTrait;

    protected $table = 'users';
}

Usage

By default you can access your eloquent date attributes in jalali date by adding a jalali_ substring to the begining of your original attribute like jalali_created_at :, (*4)

$user = Auth::user();
$user->create_at; // 2014/12/30 22:12:34
$user->jalali_created_at; // 93/09/08 22:12:34
Changing jalali_ prefix

You can change the jalali date convention prefix with overriding $model->getJalaliPrefix() and or by overrriding $model->jalaliPrefix property on your model class :, (*5)

use Diamond\JEloquent\PersianDateTrait;
use Illuminate\Database\Eloquent\Model;

class User extends Model {
    use PersianDateTrait;

    protected $jalaliPrefix = 'persian_';
}

# or

class User extends Model {
    use PersianDateTrait;

    protected function getJalaliPrefix()
    {
        // return any format you want
        return 'persian_';
    }
}
Change jalali format

You can change the jalali format $model->setJalaliFormat($format) and $model->getJalaliFormat(); or by overrriding $model->jalaliDateFormat property on your model class :, (*6)

use Diamond\JEloquent\PersianDateTrait;
use Illuminate\Database\Eloquent\Model;

class User extends Model {
    use PersianDateTrait;

    protected $jalaliDateFormat = 'l j F Y H:i';
}

# or

class User extends Model {
    use PersianDateTrait;

    public function setJalaliFormat($format){
        // do custom things here
        $this->jalaliDateFormat = $format; 

        return $this;
    }

    protected function getJalaliFormat()
    {
        // return any format you want
        return 'l j F Y H:i';
    }
}
Custom date attributes :

You can tell Eloquent that which one of your fields are date attributes like created_at and updated_at, then Eloquent treats them like Carbon objects you define multiple date attributes like this :, (*7)

use Diamond\JEloquent\PersianDateTrait;
use Illuminate\Database\Eloquent\Model;

class User extends Model {
    use PersianDateTrait;

    /**
    * Add this method to customize your date attributes
    *
    * @return array
    */
    public function getDates()
    {
        return ['created_at', 'updated_at', 'expired_at'];
    }

}

# or

class User extends Model {
    use PersianDateTrait;

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['created_at', 'updated_at', 'expired_at'];
}

When using the above trait all of the fields that are treated like date objects by Laravel will be available for conventional converting. They will be also added to model's toJson() , toArray(); and __toString(); methods., (*8)

Converter helper method

The $model->convertToPersian($attribute, $format); method allowes you to normally convert one of your fields, from Gregorian date to Jalali date :, (*9)

$user = Auth::user();
$user->convertToPersian('created_at', 'y/m/d'); // 93/09/08

The Versions

30/11 2016

dev-master

9999999-dev

Convert eloquent date attributes to Jalali (Persian) dates

  Sources   Download

GPL

The Requires

 

by Arash Farahani

30/11 2016

v1.0

1.0.0.0

Convert eloquent date attributes to Jalali (Persian) dates

  Sources   Download

GPL

The Requires

 

by Arash Farahani