/** * Run the database seeds. * * @return void */ public function run() { $regions = [["Санкт-Петербург", 15, 15], ["Уфа", 40, 40], ["Нижний Новгород", 10, 10], ["Владимир", 5, 5]]; foreach ($regions as $region) { $regionIds[] = Region::create(['title' => $region[0], 'time_to' => $region[1], 'time_back' => $region[2]])->id; } $couriers = [['Виссарион', "Иванович", "Махрыстин"], ["Ипполит", "Никифорович", "Евстигнеев"], ["Захар", "Петрович", "Перетятько"]]; foreach ($couriers as $courier) { $courierIds[] = Courier::create(['first_name' => $courier[0], 'surname' => $courier[1], 'last_name' => $courier[2]])->id; } }
/** * Run the database seeds. * * @return void */ public function run() { foreach (range(1, 40) as $i) { $date = Carbon::create(2015, 5, 1, 0, 0, 0); $courier = Courier::find(rand(1, 10)); $region = Region::find(rand(1, 10)); $start_date = $date->addDays(rand(1, 210)); $end_date = Carbon::create($start_date->year, $start_date->month, $start_date->day + $region->long, 0, 0, 0); $trips = Trip::with('courier')->where('courier_id', $courier->id)->get(); $filtered = $trips->filter(function ($trip) use($start_date, $end_date) { $trip_start = Carbon::createFromFormat('Y-m-d', $trip->start)->startOfDay(); $trip_end = Carbon::createFromFormat('Y-m-d', $trip->end)->startOfDay(); $trip_in_range = $trip_start->between($start_date, $end_date) || $trip_end->between($start_date, $end_date); $trip_over_range = $start_date->lte($trip_start) && $end_date->gte($trip_end); return $trip_in_range || $trip_over_range; }); if ($filtered->isEmpty()) { Trip::create(array('courier_id' => $courier->id, 'region_id' => $region->id, 'start' => $start_date->format('Y-m-d'), 'end' => $end_date->format('Y-m-d'))); } } }
public function postCreate(Request $request) { $this->validate($request, ['start' => 'required']); $courier = Courier::find($request->get('courier_id')); $region = Region::find($request->get('region_id')); $start_date = Carbon::createFromFormat('d.m.Y', $request->get('start')); $end_date = Carbon::create($start_date->year, $start_date->month, $start_date->day + $region->long, 0, 0, 0); $trips = Trip::with('courier')->where('courier_id', $courier->id)->get(); $filtered = $trips->filter(function ($trip) use($start_date, $end_date) { $trip_start = Carbon::createFromFormat('Y-m-d', $trip->start)->startOfDay(); $trip_end = Carbon::createFromFormat('Y-m-d', $trip->end)->startOfDay(); $trip_in_range = $trip_start->between($start_date, $end_date) || $trip_end->between($start_date, $end_date); $trip_over_range = $start_date->lte($trip_start) && $end_date->gte($trip_end); return $trip_in_range || $trip_over_range; }); if ($filtered->isEmpty()) { Trip::create(array('courier_id' => $courier->id, 'region_id' => $region->id, 'start' => $start_date->format('Y-m-d'), 'end' => $end_date->format('Y-m-d'))); return $request->ajax() ? 'Поездка успешно добавлена' : redirect()->to('/'); } else { return $request->ajax() ? 'В указанный период уже есть поездки' : redirect()->back()->withInput(); } }
public function getForm() { return view('add-trip', ['couriers' => Courier::all(), 'regions' => Region::all(), 'today' => Carbon::today()->toDateString()]); }