Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adhik13th's avatar

Laravel Set Date to d-M-Y on Carbon

i have a data and want to parsing at view .

this Table name is 'training'

id | name | user_id | no_training| date| created_at| updated_at|

and parsing view like this

1| software dev| 3| 198734-2445| 2018-06-20| 2019-06-19 19:30:40| 2019-06-19 19:30:40|

and i want to parsing at the view 'date' not = 2019-06-19 , but i want to view like , 19-June-2019 or 19-Juni-2019 (indonesian(id) Time)

what i need to add for my Controller or model ?

My Model :

   <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Training extends Model
{
    protected $table = 'training';

    protected $fillable = [
        'user_id','name','no_training','date',
    ];

    public function users()
    {
        return $this->belongsTo(\App\User::class ,'user_id');
        
    }
}

my Controller :

public function training()
{
    $training = Training::with('users')->get();
    
    return view('admin.training',['training' => $training]);
}
0 likes
9 replies
Nakov's avatar

In your Training model add this:

protected $casts = [
    'date' => 'date:d-F-Y',
];

Or you can make a getter like this:

public function getDateAttribute()
{
    return (new Carbon\Carbon($this->attributes['date']))->format('d-F-Y');
}

// or add this

protected $dates = ['date'];

public function getDateAttribute()
{
    return $this->date->format('d-F-Y');
}

Choose one of the approaches above.

Snapey's avatar

what database column type is your date column

adhik13th's avatar

@NAKOV - nice thanks for this answer is solved . i used getDateAttribute() . and now i want to add this timezone . if normal view is = 27-Jan-2020 . i live at indonesia , i add this timezone but not changed .

public function getDateAttribute() { return \Carbon\Carbon::parse($this->attributes['date']) ->timezone('Asia/Jakarta') ->format('d, M Y '); }

this output still 27-Jan-2020 not 27-Januari-2020

Nakov's avatar

@ADHIK13TH - Which one did you tried? You show an example above that your date has a value of 2019-06-19

If you try in tinker you will get this:

>>> (new Carbon\Carbon("2019-06-19"))->format('d-F-Y');
=> "19-June-2019"

So make sure that your date is not null and you are using one of the approaches I gave you above.

Snapey's avatar

you need to use localization to change the strings, not the timezone

$date = Carbon::now()->locale('fr_FR');
Nakov's avatar

@ADHIK13TH - You need localization for that, fr_FR is just an example that Snapey gave you, that's a French locale. You can take a look on other answers here for example on how to solve this.

Please or to participate in this conversation.