1

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

5
  • I assume that the method asDateTime() is something you have written, is that right? Commented Nov 16, 2023 at 17:00
  • OPTIONS: How to parse datetime using Laravel on date and time? Commented Nov 16, 2023 at 17:01
  • yes, but it does not affect on my code Commented Nov 16, 2023 at 17:01
  • @RiggsFolly there can be multiple fields on a model i want to change all at once Commented Nov 16, 2023 at 17:02
  • 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 Commented Nov 16, 2023 at 17:02

1 Answer 1

0

The serializeDate function in a Laravel model can be used to alter the date format for various date attributes. You can specify the format in which dates are serialized by using this method.

protected function serializeDate($date){
    return $date->format('Y-m-d');
}
Sign up to request clarification or add additional context in comments.

1 Comment

this code resolve the issue

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.