Search code examples
laravellaravel-9laravel-models

casts property is not working to change date format


I am trying to change date format while fetching data in this way

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

Blade

{{$ticket->due_date}}

It is showing is like

2022-03-13

Solution

  • Casts are for json serialization (json_encode($ticket)). To get the format you want in blade, use Carbon's format method.

    {{ $ticket->due_date->format('d-m-Y') }}