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

phayes0289's avatar

Handling Null Date Values in a form on Blade Template

I have a form field that is receives the following as its value:

{{ \Carbon\Carbon::parse($user->profile->doh)->format('m/d/Y H:i') }}

The value is sometime NULL. When it is NULL, it outputs the current date and time.

What is the best way to handle these type of dates in Laravel 8?

0 likes
3 replies
Sinnbeck's avatar

Add you own fallback

{{ $user->profile->doh ? \Carbon\Carbon::parse($user->profile->doh)->format('m/d/Y H:i') : 'no date' }} 
phayes0289's avatar

Thanks.. I was thinking there was something I could do in the model. I will try t that way.

Sinnbeck's avatar

@phayes0289 there is. Accessors. Here is an example on the user model, but it might make more sense on the profile model

public function getFormatDohAttribute() 
{
    return $this->profile && $this->profile->doh ? \Carbon\Carbon::parse($this->profile->doh)->format('m/d/Y H:i') : 'no date';
}

//usage
$user->format_doh;

Please or to participate in this conversation.