gpt4 book ai didi

PHP-Laravel : How to save without time overlapping in DB

转载 作者:行者123 更新时间:2023-11-29 07:18:35 28 4
gpt4 key购买 nike

我遇到一种情况,用户将在数据库中保存时间间隔意味着安排不会影响其他时间。

Eg: If a classroom is already booked for 7:00Am to 8:00am user can't save time in between that time means user can't save 7:30 to 8:30 or in that time interval.

如何检查时间间隔并在 Laravel 中保存时间?

这是我的 Controller :

 public function postAllocateRoom(Request $request)
{

$classRoom = new ClassRoom();
$classRoom->department_id=$request->Input(['department_id']);
$classRoom->room_id=$request->Input(['room_id']);
$classRoom->course_id=$request->Input(['course_id']);
$classRoom->day_id=$request->Input(['day_id']);
$classRoom->start=$request->Input(['start']);
$classRoom->end=$request->Input(['end']);
$classRoom->save();
return redirect('allocateRoomPage');
}

我的数据库中有列,名称为startend类型:Time

最佳答案

如果您想在服务器端检查它,您应该向数据库发出额外的请求以检查时间。

$startTime = Carbon::parse($request->input('start'));
$endTime = Carbon::parse($request->input('end'));

$classRoomCount = ClassRoom::where(function ($query) {
$query->where('start', '>=', $startTime)
->where('end', '<=', $startTime);
})->orWhere(function ($query) {
$query->where('start', '>=', $endTime)
->where('end', '<=', $endTime);
})->count();

if ($classRoomCount > 0) {
echo 'Classroom is already booked';
}

关于PHP-Laravel : How to save without time overlapping in DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36995959/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com