Skip to content

Instantly share code, notes, and snippets.

@afiqiqmal
Last active May 12, 2019 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afiqiqmal/be6becef5195dc01b0cd6d73387b0345 to your computer and use it in GitHub Desktop.
Save afiqiqmal/be6becef5195dc01b0cd6d73387b0345 to your computer and use it in GitHub Desktop.
Auto generate Date Formatted Attribute for Laravel Model

Date Formatted

This Gist is for reduce appends item in laravel model which provide auto create attribute for dates to be formatted

Basic Append

protected $appends = [
    ..
    ..
    'updated_at_formatted',
    'created_at_formatted',
    'post_created_time_formatted'
];

Then, need to create function for the updated_at_formatted name getUpdatedAtFormattedAttribute()

Date Formatted Style

  • No need to appends or create the attribute function. see Announcement.php
// which column to make it formatted<br>
protected $column_date = ['post_created_time', 'created_at', 'updated_at'];<br>
// how the date column is format<br>
protected $column_date_formatted = ['M d Y, H:i', 'd-m-Y h:i:s a', 'd-m-Y h:i:s a'];<br>
// all column will followed the same format<br>
// protected $column_date_formatted = 'M d Y, H:i';<br>
<?php
/**
* Created by PhpStorm.
* User: hafiq
* Date: 14/05/2018
* Time: 9:28 AM
*/
namespace App\Models\Traits;
use Carbon\Carbon;
trait DateFormatted
{
protected $column_date_formatted = 'd-m-Y H:i:s';
protected $column_date = ['created_at', 'updated_at'];
public static function bootDateFormatted()
{
static::retrieved(function ($model) {
if (is_array($model->column_date_formatted)) {
if (count($model->column_date_formatted) != count($model->column_date)) {
throw new \RuntimeException('If Column date Formatted is array,
the formatted array count should be the same as column date');
}
}
if (!is_array($model->column_date)) {
$model->column_date = [$model->column_date];
}
foreach ($model->column_date as $key => $item) {
$newItem = $item.'_formatted';
if (is_array($model->column_date_formatted)) {
$model->$newItem = Carbon::parse($model->$item)->format($model->column_date_formatted[$key]);
} else {
$model->$newItem = Carbon::parse($model->$item)->format($model->column_date_formatted);
}
}
});
static::updating(function ($model) {
foreach ($model->column_date as $key => $item) {
unset($model->{$item.'_formatted'});
}
});
static::creating(function ($model) {
foreach ($model->column_date as $key => $item) {
unset($model->{$item.'_formatted'});
}
});
}
}
<?php
/**
* Created by PhpStorm.
* User: hafiq
* Date: 14/05/2018
* Time: 9:28 AM
*/
namespace App\Models\Traits;
use Illuminate\Database\Eloquent\Model;
class DateFormattedModel extends Model
{
use DateFormatted;
}
<?php
namespace App\Models;
use App\Models\Traits\DateFormattedModel;
class Test extends DateFormattedModel
{
// which column to make it formatted
protected $column_date = ['post_created_time', 'created_at', 'updated_at'];
// how the date column is format
protected $column_date_formatted = ['M d Y, H:i', 'd-m-Y h:i:s a', 'd-m-Y h:i:s a'];
// protected $column_date_formatted = 'M d Y, H:i'; // all column will followed the same format
...
...
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment