/** * Run the database seeds. * * @return void */ public function run() { for ($i = 1; $i <= 3; $i++) { $partner = Partner::find($i); $event = Event::find(1); $distance = LocationController::getMapsMatrixDistance($event->location, $partner->location); $event->partners()->save($partner, ['distance' => $distance]); } }
public function update($id, Request $request) { // Ensure the user is logged in as an admin if (!Auth::check() || !Auth::user()->is_admin) { return response(view('errors.403', ['error' => $this->errorMessages['incorrect_permissions']]), 403); } // Try to retrieve the model for updating from the database try { $event = Event::findOrFail($id); } catch (ModelNotFoundException $e) { return Redirect::back()->withErrors(['message' => $this->errorMessages['event_not_found']]); } // Get the required fields from the form $data = $request->only(['title', 'tagline', 'description', 'partner_id', 'featured_image', 'start_date', 'end_date', 'start_time', 'end_time', 'location_id', 'price']); // Validate the data $validator = Validator::make($data, ['title' => 'required', 'tagline' => 'required', 'description' => 'required', 'start_date' => 'required', 'end_date' => 'required', 'start_time' => 'required', 'end_time' => 'required', 'location_id' => 'required', 'price' => 'required|numeric|min:0', 'featured_image' => 'image|sometimes']); // If validation fails; if ($validator->fails()) { // Redirect back to registration form with errors return Redirect::back()->withErrors($validator)->withInput(); } // Format the start and end datetime properly // for storage in the database $start_datetime = $data['start_date'] . ' ' . $data['start_time'] . ':' . '00'; $end_datetime = $data['end_date'] . ' ' . $data['end_time'] . ':' . '00'; $newData = array("title" => $data["title"], "tagline" => $data["tagline"], "description" => $data["description"], "start_datetime" => $start_datetime, "end_datetime" => $end_datetime, "location_id" => $data["location_id"], "price" => $data["price"]); // Only if the user updated the photo; if ($request->hasFile('featured_image')) { // Upload the image $data['featured_image'] = MediaController::uploadImage($request->file('featured_image'), time(), $directory = "event_photos", $bestFit = true, $fitDimensions = [1920, 1080]); // Store the new image $event->featured_image = $data['featured_image']; } else { unset($data['featured_image']); } // Store the values from the form in event $event->update($newData); // Detach all partners and attach new partners to event $event->partners()->detach(); $distance; if (isset($data['partner_id']) && $data['partner_id'] != '') { foreach ($data['partner_id'] as $partner_id) { $distance = LocationController::getMapsMatrixDistance(Partner::find($partner_id)->location, $event->location); $event->partners()->attach($partner_id, ['distance' => $distance]); } } // Save the event to the database $event->save(); // Return to the events index return Redirect::route('events.show', $event->id); }