© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•15mo ago•
2 replies
Raven

Validation for overlapping date/time fields

Hello! I've got a
TimeLog
TimeLog
resource which is for users to clock in and clock out. How do I make sure that the
clock_in
clock_in
and
clock_out
clock_out
timestamps are not overlapping with any other entry in the database for that user? I've got a validation rule right now that I'm applying to both of the timestamp fields, but I don't like how messy it is and was wondering if there's a better way:
$noOverlappingClocks = fn(Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
    $userId = $get('user_id');
    $clockIn = $get('clock_in');
    $clockOut = $get('clock_out');

    if ($clockIn && $clockOut) {
        $overlap = TimeLog::query()
            ->where('user_id', $userId)
            ->where(function ($query) use ($clockIn, $clockOut) {
                $query->whereBetween('clock_in', [$clockIn, $clockOut])
                    ->orWhereBetween('clock_out', [$clockIn, $clockOut])
                    ->orWhere(function ($query) use ($clockIn, $clockOut) {
                        $query->where('clock_in', '<', $clockIn)
                            ->where('clock_out', '>', $clockOut);
                    });
            })
            ->exists();

        if ($overlap) {
            $fail("The provided time log overlaps with an existing entry.");
        }
    }
};
$noOverlappingClocks = fn(Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
    $userId = $get('user_id');
    $clockIn = $get('clock_in');
    $clockOut = $get('clock_out');

    if ($clockIn && $clockOut) {
        $overlap = TimeLog::query()
            ->where('user_id', $userId)
            ->where(function ($query) use ($clockIn, $clockOut) {
                $query->whereBetween('clock_in', [$clockIn, $clockOut])
                    ->orWhereBetween('clock_out', [$clockIn, $clockOut])
                    ->orWhere(function ($query) use ($clockIn, $clockOut) {
                        $query->where('clock_in', '<', $clockIn)
                            ->where('clock_out', '>', $clockOut);
                    });
            })
            ->exists();

        if ($overlap) {
            $fail("The provided time log overlaps with an existing entry.");
        }
    }
};


It also does error when editing an existing entry even tho it only "overlaps" with itself.
Solution
I'd use a customv aldiation rule, also see:

https://laracasts.com/discuss/channels/filament/rule-in-filamentphp
Laracasts
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Fields validation
FilamentFFilament / ❓┊help
3y ago
Fail validation for multiple fields
FilamentFFilament / ❓┊help
2y ago
Date Filter Validation
FilamentFFilament / ❓┊help
3y ago
Date-time picker
FilamentFFilament / ❓┊help
3y ago