skip to Main Content

Hello I have laravel project that store data created_at as a timestamp, I would call it from database, but it still show timestamp format, how can I convert it to date format without changing my column type? image

here is my model:

public function getCreatedAtAttribute($value)
{
    return Carbon::parse($value)->timestamp;
}

//fungsi untuk merubah format tanggal diubah ke timestamp
public function getUpdatedAtAttribute($value)
{
    return Carbon::parse($value)->timestamp;
}

and how should I call it in view or .blade file??
Thank you

4

Answers


  1. To convert to human readable string try changing your functions as below:

    public function getCreatedAtAttribute($value)
    {
        return Carbon::parse($value)->toFormattedDateString();
    }
    
    //fungsi untuk merubah format tanggal diubah ke timestamp
    public function getUpdatedAtAttribute($value)
    {
        return Carbon::parse($value)->toFormattedDateString();
    }
    

    toFormattedDateString() function will convert to format like Jun 22, 2020. See this article for more alternatives https://www.leonelngande.com/format-dates-for-humans-with-carbon-in-php/.

    To use in blade file simply access the parameter like:

    $modelvariablename->created_at
    
    Login or Signup to reply.
  2. In your model:

    public function getCreatedAtAttribute(): Carbon {
        return Carbon::parse($this->created_at);
    }
    
    public function getUpdatedAtAttribute(): Carbon {
        return Carbon::parse($this->created_at);
    }
    

    For use:

    $model->created_at->format('d.m.Y');
    
    Login or Signup to reply.
  3. If you pass the data from controller to blade You can call in your blade file like this …

    <sapn>{{ $variable->updated_at->toDateString() }}</span>
    

    Or

    <span>{{ $variable->updated_at->format('Y-m-d') }}</span>
    

    if you want to convert to inside controller.

    public function getCreatedAtAttribute($value)
    {
        return $value->toDateString();
    }
    

    fungsi untuk merubah format tanggal diubah ke timestamp

    public function getUpdatedAtAttribute($value)
    {
        return $value->toDateString();
    }
    

    Or you can try carbon

    use CarbonCarbon;
        
    public function getCreatedAtAttribute($value)
    {
        return Carbon::parse($value)->format('Y-m-d');
    }
    

    fungsi untuk merubah format tanggal diubah ke timestamp

    public function getUpdatedAtAttribute($value)
    {
        return Carbon::parse($value)->format('Y-m-d');
    }
    
    Login or Signup to reply.
  4. I would suggest to use eloquent’s attribute casting instead.

    So for example, make created_at shown as date in d-m-Y format:

    protected $casts = [
        'created_at' => 'datetime:d-m-Y'
    ];
    

    Note that attribute casting will only effects the attibute when the model is serialized to json or array (as stated in the doc).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search