I'm attempting to retrieve data from the database that falls between $booking_start_formatted and $booking_end based on the session_start column. However, it only selects data earlier than $booking_start_formatted.
public function store(Request $request, Utils $utils)
{
$request->validate([
"user_id" => "required|int",
"booking_start" => "required",
]);
try {
$booking_start = Carbon::parse($request->get("booking_start"));
$booking_start_formatted = $booking_start->format("Y-m-d H:i:s");
$booking_end = $booking_start->addMinute(45)->format("Y-m-d H:i:s");
$booking = Bookings::query()->whereBetween("session_start", [$booking_start_formatted, $booking_end])->get();
return $utils->message("error", $booking, 400);
} catch (\Throwable $e) {
// Handle the exception
return $utils->message("error", $e->getMessage(), 400);
}
}
The timestamp format I'm using is '2024-03-31 06:35:13'.
You need to clone the
Carbonobject or change to immutable object. Carbon is a mutable object by default.Use
copyorclonemethod :Use
object cloningmethod : php.netLook this Stack Overflow post, same issue https://stackoverflow.com/a/49905830/11836673