/** * Store a newly created resource in storage. * * @param Request $request * * @return Response */ public function store(Request $request, Trip $trip) { $company = Auth::travel_company_staff()->get()->travel_company; $stations = $company->stations->lists('city')->toArray(); $dps = $request->departure_station; $des = $request->destination_station; $this->validate($request, ['departure_station' => "required | different:destination_station", 'departure_date' => "required", 'departure_time' => 'required', 'destination_station' => "required", 'fare' => 'required', 'transport_model' => 'required', 'hour' => 'integer|required', 'minute' => 'integer|required', 'boarding_point' => 'required', 'number_of_seats' => 'required|integer']); if (!in_array($dps, $stations) or !in_array($des, $stations)) { return redirect()->back()->withInput()->withErrors('Invalid Station'); } $departure_date = $this->carbonize($request->departure_date); if ($departure_date->lte(Carbon::tomorrow())) { return redirect()->back()->withInput()->withErrors('Invalid Departure Date'); } $min = $request->minute; $hour = $request->hour; $duration = $this->durationize($hour, $min); $slug = str_slug($request->departure_station . ' to ' . $request->destination_station); $code = $trip->code(); $input = array_add($request->except(['minute', 'hour']), 'code', $code); $input['departure_date'] = $departure_date; $input = array_add($input, 'travel_company_id', $company->id); $input = array_add($input, 'duration', $duration); $input = array_add($input, 'slug', $slug); Trip::create($input); return redirect()->route('company.trips.index'); }
/** * @param int $courier_id * @param int $region_id * @param Carbon | string $departure_date * @return Trip */ public function createTrip($courier_id, $region_id, $departure_date) { if (!$departure_date instanceof Carbon) { $departure_date = Carbon::createFromFormat('Y-m-d', $departure_date); } $regions = Region::all(); $arrivalDate = clone $departure_date; $arrivalDate->addHours($regions->find($region_id)->time_to); $returnDate = clone $arrivalDate; $returnDate->addHours($regions->find($region_id)->time_back); return Trip::create(['courier_id' => $courier_id, 'region_id' => $region_id, 'departure_date' => $departure_date, 'arrival_date' => $arrivalDate, 'return_date' => $returnDate]); }
/** * 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(); } }
/** * create Trip with user id * * @param $attr * @param $id * @return static */ public static function createWithUserId($attr, $id) { $attr['user_id'] = $id; return Trip::create($attr); }
/** * Save a new trip. * * @param TripRequest $request * @return Response */ public function store(TripRequest $request) { Trip::create($request->all()); return redirect('admin/trips')->with(['flash_message' => 'Viagem adicionada', 'flash_message_level' => 'success']); }