首页 > 解决方案 > Laravel 时间范围

问题描述

我正在使用 Laravel 5.8,我有两列数据类型为时间戳,start_time 和 end_time。在保存新记录之前,我想检查它是否已经存在。

例如,如果我的数据库中已经有Start Time= 09:00:00, End Time: 17:00:00我不应该设置 start time = 13:00:00 和 end_time=16:00:00 因为它已经在现有的时间间隔内,我也不应该注册16:00:00->20:00:00因为 16:00:00 在时间间隔内。

到目前为止,我有这个,它不起作用:

DB::table('student_classes')
    ->where('id', '!=', $id)
    ->whereNotBetween('start_date', [$data['start_date'], $data['end_date']]);

标签: mysqllaraveleloquentlaravel-5.8

解决方案


@MahdiMiad 您如何在数据库中节省时间-它是时间戳还是字符串?我希望你使用时间戳。

在保存之前进行查询。

$rows = DB::table('student_classes')->whereNotBetween('start_date', [$data['start_date'],$data['end_date']])->get();
if (count($rows)>0) {
    // throw error
} else {
    // save the results
}

推荐阅读