Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Snowfox1991's avatar

Have validate the date inputted for blocked day (type-2) but cannot add the data

0 down vote favorite I have validated the exception when people input the leave_from_date and leave_to_date that contains blocked day, but when i change to the new date, i cant add a new LeaveRequest. This is my code from LeaveRepository

public function create($inputs){

    $events = DB::table('events')
    ->where('event_type_id', 2)
    ->get();

    $from_dates = $inputs['leave_from_date']; 
    $to_dates = $inputs['leave_to_date']; 

    foreach ($events as $event) {
        foreach ($from_dates as $key => $from_date) {
            if (($from_date >= $event->from_date && $from_date <= $event->to_date)
                || ($to_dates[$key] >= $event->from_date && $to_dates[$key] <= $event->to_date)) {
                return false;
            }
        }

    }
    return $this->model->create(request()->all());
}

I also when testing with postman, i can only use Form-data with leave_from_date[] and leave_to_date[] like this. It is inconvenient for me. Is there anyway to handle the input better? When i change the leave_from_date and leave_to_date like this in form, it got error

Invalid argument supplied for foreach() Is there anyway to handle input with validation like this. I have validated success that days contains blocked days will be changed but cannot add. Thank you

My input for LeaveRequest is emp_id, leave_type, leave_from_date, leave_to_date. When i input leave_from_date and leave_to_date it is validated the blocked day, but when i input full information, it is not added.

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

because you are trying to use $from_dates as an array when its probably just a string

(or you have made it an array with [] on the end of the form field name) but its still a single value

If it is just a string, you cannot just string compare it to another date. You need to convert it with php date object or Carbon

1 like
Snowfox1991's avatar

Yes i have created. Thank you so much. I have changed the code because i forgot to convert

Please or to participate in this conversation.