Dazzle
Dazzle

Reputation: 59

How To Validate Crossed Time Laravel

I have the time data in my database like this

+-------------+------------+------------+
|    id       | time_start | time_end   |
+---------------------------------------+
|    1        |  07:00     |  10:00     |
|    2        |  12:00     |  14:00     |
|             |            |            |
|             |            |            |
|             |            |            |
|             |            |            |
|             |            |            |
|             |            |            |
+-------------+------------+------------+

but i'm insert time crossed,

time_start : 08:00
time_end : 09:00

or

time_start : 08:00
time_end : 11:00

How to validate if the time_start and time_end not between date in database ?

Upvotes: 0

Views: 61

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89565

I give you the way to do that in raw SQL, feel free to translate it into eloquent rigmarole:

SET @tstart='10:00';
SET @tend='11:00';

INSERT INTO t_time_range (time_start, time_end)
SELECT @tstart, @tend 
WHERE  NOT EXISTS
       ( SELECT 1 
         FROM   t_time_range
         WHERE  time_start >= @tstart AND time_start < @tend
         OR     time_end > @tstart    AND time_end <= @tend
         OR     @tstart >= time_start AND @tstart < time_end
       );

Upvotes: 1

user379888
user379888

Reputation:

Suppose your model is time, the time you want to enter are $time_start and $end_time.

$time = DB::table('time')
->where(['time_start', $time_start], 
        ['time_end', $end_time])
->first(); // see if there already is an entry

If $time has no result, enter the time in database.

Upvotes: 0

Related Questions