-
app/Rules/TimeIntervalDoesNotExist.php
Open in GitHubuse 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 GitHubuse 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), ), ], // ... ]; } }