-1

How can I simplifyy this code of mine?

enter image description here

tereško
  • 58,060
  • 25
  • 98
  • 150
ghabriel
  • 123
  • 9

1 Answers1

1

You can save the result of Carbon::*() into a variable and call its members.

if (empty($startDate)) {
    $carbon = Carbon::now();
} else {
    $carbon = Carbon::createFromFormat(...);
}
$post->start_at = $carbon;
if (empty($endDate)) {
    $post->end_at = $carbon->endOfDay(); 
} else {
    $post->end_at = Carbon::createFromFormat(...);
}
Jonathan Rosa
  • 992
  • 7
  • 22
  • I have to edit it a little, $post must be saved first before next if else otherwise it's timestamp will change – ghabriel May 14 '20 at 01:30