function postThemmoi() { // Get data $input['name'] = Input::get('name'); $input['start_time'] = Input::get('start_time'); $input['end_time'] = Input::get('end_time'); // Validate $rules = ['name' => 'required|string|min:2|max:16', 'start_time' => 'required|date_format:H:i:s', 'end_time' => 'required|date_format:H:i:s']; $validator = Validator::make($input, $rules); if ($validator->fails()) { $errors_validate = $validator->messages()->all(); return back()->with('errors_validate', $errors_validate)->withInput(); } // If validate Ok $the_shift = Shift::create($input); add_message('Tạo ca học mới thành công'); return redirect()->route('cahoc'); }
public function create(ShiftRequest $request) { $this->authorize('create-shift'); $input = $request->all(); $department = Department::find($input['department_id']); if (isset($input['roles'])) { // Convert roles into JSON $input['roles'] = json_encode($input['roles']); // Check if the current roles match the department roles if ($input['roles'] == $department->roles) { // Unset the roles, use department as default instead unset($input['roles']); } } // Set start and end dates if not included $input = Shift::setDates($department, $input); $input = Shift::setTimes($input); $shift = Shift::create($input); // Generate slots based on shift options Slot::generate($shift); event(new EventChanged($department->event, ['type' => 'shift', 'status' => 'created'])); $request->session()->flash('success', 'Your shift has been created.'); return redirect('/event/' . $department->event->id); }
public function cloneEvent(Request $request, Event $event) { $this->authorize('create-event'); $this->validate($request, ['start_date' => 'required|date_format:Y-m-d']); // Set up event information $startDate = new Carbon($event->start_date); $endDate = new Carbon($event->end_date); $newStartDate = new Carbon($request->input('start_date')); $departments = $event->departments; // Find the difference of the start dates $difference = $startDate->diffInSeconds($newStartDate); // Create new event from old event data $newEvent = Event::create(['name' => $event->name, 'description' => $event->description, 'start_date' => $startDate->addSeconds($difference)->format('Y-m-d'), 'end_date' => $endDate->addSeconds($difference)->format('Y-m-d')]); // Add the image manually because it's not automatically fillable if ($event->image) { $newEvent->image = $event->image; $newEvent->save(); } // Loop through event departments foreach ($departments as $department) { // Create new department $newDepartment = new Department(); $newDepartment->event_id = $newEvent->id; $newDepartment->save(); // Because the event_id isn't fillable, we have to define it first and then update $newDepartment->update(['name' => $department->name, 'description' => $department->description, 'roles' => $department->roles]); // Loop through shifts $shifts = $department->shifts; foreach ($shifts as $shift) { // Adjust shift dates $shift->start_date = new Carbon($shift->start_date); $shift->end_date = new Carbon($shift->end_date); $shift->start_date = $shift->start_date->addSeconds($difference)->format('Y-m-d'); $shift->end_date = $shift->end_date->addSeconds($difference)->format('Y-m-d'); // Update the department ID $shift->department_id = $newDepartment->id; $newShift = Shift::create(['department_id' => $newDepartment->id, 'name' => $shift->name, 'start_date' => $shift->start_date, 'end_date' => $shift->end_date, 'start_time' => $shift->start_time, 'end_time' => $shift->end_time, 'duration' => $shift->duration, 'roles' => $shift->roles]); Slot::generate($newShift); } } $request->session()->flash('success', 'Event has been cloned.'); return redirect('/event/' . $newEvent->id); }