seiler/eloquent-date

A trait for Eloquent models to help you work with dates in different languages

5.6.0 2018-05-13 11:07 UTC

This package is auto-updated.

Last update: 2021-05-19 14:03:44 UTC


README

Latest Version Software License

This trait replaces Carbon with Jenssegers\Date in Laravel's Eloquent model.

Jenssegers\Date extends Carbon with multi-language support. Methods such as format, diffForHumans, parse, createFromFormat and the new timespan, will now be translated based on your locale.

This package is compliant with PSR-1, PSR-2 and PSR-4. If you notice compliance oversights, please send a patch via pull request.

Laravel compatibility

Laravel Eloquent-Date
5.6 5.6
5.5 5.5
5.4 5.4
5.3 5.3
5.2 5.2

Installation

Install using composer:

$ composer require seiler/eloquent-date

If you want to use Jenssegers\Date in other part of your application, there is a service provider included in the package for integration with the Laravel framework. This provider will get the application locale setting and use this for translations. To register the service provider, add the following to the providers array in config/app.php:

'providers' => [
    ...
    /*
     * Application Service Providers...
     */
    Jenssegers\Date\DateServiceProvider::class,
    ...
];

You can also add it as a Facade in config/app.php:

'aliases' => [
    ...
    'Date' => Jenssegers\Date\Date::class,
    ...
];

Languages

The Date package contains language files for the following languages:

  • Albanian
  • Arabic
  • Azerbaijani
  • Bangla
  • Basque
  • Brazilian Portuguese
  • Bulgarian
  • Catalan
  • Croatian
  • Chinese Simplified
  • Chinese Traditional
  • Czech
  • Danish
  • Dutch
  • English
  • Esperanto
  • Estonian
  • Finnish
  • French
  • Galician
  • Georgian
  • German
  • Greek
  • Hebrew
  • Hindi
  • Hungarian
  • Icelandic
  • Indonesian
  • Italian
  • Japanese
  • Kazakh
  • Korean
  • Latvian
  • Lithuanian
  • Macedonian
  • Malay
  • Norwegian
  • Nepali (नेपाली)
  • Polish
  • Portuguese
  • Persian (Farsi)
  • Romanian
  • Russian
  • Thai
  • Serbian (latin)
  • Serbian (cyrillic)
  • Slovak
  • Slovenian
  • Spanish
  • Swedish
  • Turkish
  • Turkmen
  • Ukrainian
  • Uzbek
  • Vietnamese
  • Welsh

Usage

In your Eloquent model, add the EloquentDate trait:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use \Seiler\EloquentDate\EloquentDate;

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

Now, any attribute declared in $dates will be converted to Jenssegers\Date instance instead of Carbon:

\Jenssegers\Date\Date::setLocale('fr');

$post = Post::find(1);

echo $post->published_at->format('l j F Y H:i:s'); // samedi 19 mars 2016 21:58:16

echo $post->published_at->ago(); // il y a 1 jour

To learn all you can do with Jenssegers\Date, please refer to its own documentation.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email frederic@seiler.io instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.