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

SigalZ's avatar

Dates formatting

Hello, I have a model with a few date fields in it.

I have this in the model:

protected $dates = ['request_date', 'jobs_done_date', 'sent_for_invoice_date'];

protected $casts = [
        'request_date' => 'date:D d/m/Y',
];

I added the request_date in the $casts because I need to get it with this code:

    {{ Arr::get($model->toArray(), $col->attribute) }}

And if I don't do that, the date is displayed like this:

2021-12-11T00:00:00.000000Z

Now, I have a view where I need to display the 'jobs_done_date'. If I do this:

$job->jobs_done_date->format('d/m/Y')

I get an error when the date is null.

So I tried this function in the model:

public function getJobsDoneDateAttribute($value)
    {
        return is_null($value) ? 'Not completed' : $value->format('d/m/Y');
    }

That works fine when the date is null, but when it's not, I get this error:

Call to a member function format() on string

How do I solve this?

0 likes
9 replies
Sinnbeck's avatar

Yeah you are overwriting the cast. Just do this

public function getJobsDoneDateAttribute($value)
    {
        return is_null($value) ? 'Not completed' : \Carbon\Carbon::parse($value)->format('d/m/Y');
    } 
1 like
SigalZ's avatar

@Sinnbeck Thank you, that works 100% but I am curious about drewdan answer, although I couldn't make it work from the model. It works if I put his code in the blade file.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@SigalZ it seems to suffer the same problem with casting. You can use another name

public function getJobsDoneDateFormattedAttribute()
    {
        return  optional($this->jobs_done_date)->format('d/m/Y') ?? 'Not completed';
    }

//usage
{{$job->jobs_done_date_formatted}} 
1 like
Sinnbeck's avatar

@SigalZ that sounds weird. So this shows the text and the date next to each other?

{{$job->jobs_done_date_formatted}}
{{$job->jobs_done_date}} 
SigalZ's avatar

@Sinnbeck Hi, I was working on other things all day long. So now, trying your solution again, suddenly it works... Not sure what is going on, but for now, it works, so thank you very much!

drewdan's avatar

There is also a helper function that can help with this too. Either way works, but I like this helper, can be useful in lots of places.

public function getJobsDoneDateAttribute($value)
    {
        return  optional($value)->format('d/m/Y') ?? 'Not completed';
    }

It basically evaluates the item in the parens, and if it's null, stops the chain, and then you can add a ternary to it

1 like
SigalZ's avatar

@drewdan Thank you. I tried your code but it returns 'Not Completed' even though the field has a date in it in the database.

The strange thing is, if I put your code straight in the blade file, it works fine.

I read about the optional helper and tried this code in the Model:

public function getJobsDoneDateAttribute($value)
{
       return optional($value, function ($value) {
            return $value->format('D d/m/Y');
        });
}

But this gives the error: Call to a member function format() on string

You can probably see I don't fully understand the purpose of protected $dates = ['request_date', 'jobs_done_date'];

I thought it turns the date into a Carbon? So why do I get the error?

Please or to participate in this conversation.