public function store(Request $request)
 {
     $rules = ['school_year_id' => 'required|exists:school_years,id', 'name' => 'required|min:4|max:255', 'start' => 'required|date', 'end' => 'required|date'];
     $messages = ['name.required' => 'Es necesario asignar un nombre al periodo.', 'name.min' => 'Ingrese un nombre adecuado.', 'start.required' => 'Es necesario definir la fecha de inicio.', 'end.required' => 'Es necesario definir la fecha de fin.'];
     $v = Validator::make($request->all(), $rules, $messages);
     if ($v->fails()) {
         return back()->withErrors($v)->withInput();
     }
     // Custom validations
     if ($request->get('start') > $request->get('end')) {
         $errors['range'] = 'Las fechas son inconsistentes.';
     }
     $year = SchoolYear::find($request->get('school_year_id'));
     if ($request->get('start') < $year->start) {
         $errors['start'] = 'La fecha de inicio debe ser posterior al inicio del año lectivo.';
     }
     if ($request->get('end') > $year->end) {
         $errors['end'] = 'La fecha de fin debe ser anterior al fin del año lectivo.';
     }
     if (isset($errors)) {
         return back()->withErrors($errors)->withInput();
     }
     Period::create($request->all());
     return back()->with('success', 'Periodo lectivo registrado exitosamente.');
 }
Пример #2
0
 public function run()
 {
     DB::table('periods')->delete();
     Period::create(['id' => 1, 'start' => '2015-11-10 12:35:00', 'end' => '2015-11-11 12:36:00']);
     Period::create(['id' => 2, 'start' => '2015-11-11 12:36:00', 'end' => '2015-11-11 12:37:00']);
     Period::create(['id' => 3, 'start' => '2015-11-11 12:37:00', 'end' => '2015-11-11 12:38:00']);
     Period::create(['id' => 4, 'start' => '2015-11-11 12:38:00', 'end' => '2015-11-11 12:39:00']);
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $carbon = Carbon::now('America/Lima');
     // Current year
     $year = $carbon->year;
     // Getting the current school year
     $schoolYear = SchoolYear::all()->last();
     // Calculating the middle date
     $middle = clone $schoolYear->start;
     $middle->addMonths(6);
     // First period of the current school year
     Period::create(['school_year_id' => $schoolYear->id, 'name' => $year . '-I', 'start' => $schoolYear->start, 'end' => $middle]);
     // Last period of the current school year
     Period::create(['school_year_id' => $schoolYear->id, 'name' => $year . '-II', 'start' => $middle->addDay(), 'end' => $schoolYear->end]);
 }