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

phayes0289's avatar

Is this the best way to handle null date values involving Carbon in the update Controller?

I have a set of data that contains three date values that are NOT required. I have the following code working, but I am wondering if there is anything more condensed / efficient when programming in Laravel 8.

   $profile->empnumber = $request->empnumber;
        $profile->altempnumber = $request->altempnumber;
        $profile->altemail = $request->altemail;
        $request->dob !== null ? $profile->dob = Carbon::parse($request->dob ?? NULL) : $profile->dob=null;
        $request->doh !== null ? $profile->doh = Carbon::parse($request->doh ?? NULL) : $profile->doh=null;
        $request->dos !== null ? $profile->dos = Carbon::parse($request->dos ?? NULL) : $profile->dos=null;
        $profile->separation_reason = $request->separation_reason;

Thanks

0 likes
3 replies
MichalOravec's avatar
$profile->dob = $request->dob ? Carbon::parse($request->dob) : null;

And the same for other dates: doh, dos.

Snapey's avatar

just to show there are always multiple solutions with Laravel

$profile->dob = $request->filled('dob') ? Carbon::parse($request->dob) : null ;

Please or to participate in this conversation.