Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

cattr-app/server-application

87 stars
2 code files
View cattr-app/server-application on GitHub

app/Rules/TimeIntervalDoesNotExist.php

Open in GitHub
use App\Models\User;
use App\Models\TimeInterval;
use Carbon\Carbon;
use Illuminate\Contracts\Validation\Rule;
 
class TimeIntervalDoesNotExist implements Rule
{
private ?User $user;
 
private Carbon $startAt;
 
private Carbon $endAt;
 
public function __construct(?User $user, Carbon $startAt, Carbon $endAt)
{
$this->user = $user;
$this->startAt = $startAt;
$this->endAt = $endAt;
}
 
public function passes($attribute, $value): bool
{
return !TimeInterval::where('user_id', optional($this->user)->id)
->where(function ($query) {
$query
->whereBetween('start_at', [$this->startAt, $this->endAt])
->orWhereBetween('end_at', [$this->startAt, $this->endAt])
->orWhere(function ($query) {
$query
->where('start_at', '<', $this->startAt)
->where('end_at', '>', $this->endAt);
});
})
->exists();
}
 
public function message(): string
{
return trans('validation.time_interval_does_not_exist');
}
}

app/Http/Requests/Interval/CreateTimeIntervalRequest.php

Open in GitHub
use App\Http\Requests\CattrFormRequest;
use App\Models\User;
use App\Rules\TimeIntervalDoesNotExist;
use Carbon\Carbon;
use Settings;
 
class CreateTimeIntervalRequest extends CattrFormRequest
{
// ...
 
public function _rules(): array
{
$timezone = Settings::scope('core')->get('timezone', 'UTC');
 
return [
// ...
'end_at' => [
'required',
'date',
'bail',
'after:start_at',
new TimeIntervalDoesNotExist(
User::find($this->user_id),
Carbon::parse($this->start_at)->setTimezone($timezone),
Carbon::parse($this->end_at)->setTimezone($timezone),
),
],
// ...
];
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.