public function store(Request $request) { if (Room::count() == 0) { flash()->error("You must add at least one room before creating\n a conference"); return back()->withInput(); } $this->validate($request, ['name' => 'required|min:5|max:255']); $first = $request['start_time']; $last = $request['end_time']; if (empty($first[0]) or empty($last[0])) { flash()->error("You must add a start time and an end time"); return back()->withInput(); } $conference = new Conference(); $conference->name = $request['name']; $conference->save(); $rooms = Room::where('available', true)->get(); $numDays = sizeOf($first); //start day loop from here if ($numDays == 1) { $times = []; $tStart = strtotime($first[0]); $tEnd = strtotime($last[0]); $tNow = $tStart; $i = 1; $times[0] = date("H:i", $tNow) . "\n"; while ($tNow < $tEnd) { $tNow = strtotime('+30 minutes', $tNow); $times[$i] = date("H:i", $tNow) . "\n"; $i = $i + 1; } foreach ($rooms as $room) { foreach ($times as $time) { $timeslot = new Timeslot(); $timeslot->day = 1; $timeslot->room_code = $room->code; $timeslot->conference_id = $conference->id; $timeslot->time = $time; $timeslot->save(); } } } else { for ($day = 1, $index = 0; $day < $numDays || $index < $numDays - 1; $day++, $index++) { $times = []; $tStart = strtotime($first[$index]); $tEnd = strtotime($last[$index]); $tNow = $tStart; $i = 1; $times[0] = date("H:i", $tNow) . "\n"; while ($tNow < $tEnd) { $tNow = strtotime('+30 minutes', $tNow); $times[$i] = date("H:i", $tNow) . "\n"; $i = $i + 1; } //$i os the number of timeslots foreach ($rooms as $room) { foreach ($times as $time) { $timeslot = new Timeslot(); $timeslot->day = $day; $timeslot->room_code = $room->code; $timeslot->conference_id = $conference->id; $timeslot->time = $time; $timeslot->save(); } } } } flash()->success("New conference created successfully!!"); return redirect()->route('user.show'); }