How can I simplifyy this code of mine?
Asked
Active
Viewed 53 times
1 Answers
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