How to customize date format for multiple date attributes in Laravel Model? [duplicate]

I have a Laravel Model named CostingSheetFabric with several date attributes defined in the $dates property, such as customer_start_date, customer_cancel_date, earliest_start_date, and earliest_cancel_date. I’m looking to customize the date format for these attributes when they are retrieved from the database.

I’ve tried using the asDateTime method in my model like this:

This seems not work for my use case, but I wanted to seek advice from the community. Is using asDateTime a recommended approach for customizing the date format in Laravel models, especially when dealing with multiple date attributes?

protected function asDateTime($value)
{
    return parent::asDateTime($value)->format('Y-m-d');
}

following are not possible a model may have 35 date columns

protected $dates = [
    'custom_date',
];

 public function getStartDateAttribute($value)
 {
     return Carbon::parse($value)->format("Y-m-d");
 }

tired this but does not working

  • I assume that the method asDateTime() is something you have written, is that right?

    – 

  • OPTIONS: How to parse datetime using Laravel on date and time?

    – 




  • yes, but it does not affect on my code

    – 

  • @RiggsFolly there can be multiple fields on a model i want to change all at once

    – 

  • Then it would be rather useful to know what was actually coded in that method. Otherwise you are asking “How long is a piece of string” … Difficult to answer as no where near enough information is provided

    – 

Leave a Comment