/** * deletes item and according image, also updates trip and entry that use this picture as featured image * * @param $user_id * @throws \Exception */ public function deleteWithImage($user_id) { if ($entry = TripEntry::where('pic', $this->id)->first()) { $entry->pic = null; $entry->save(); } if ($trip = Trip::where('pic', $this->id)->first()) { $trip->pic = null; $trip->save(); } //deleting image as well File::delete(public_path('img') . '/' . $user_id . '/' . $this->filename); $this->delete(); }
/** * create a new TripEntry with Trip_id set * * @param $attr * @param $id * @return static */ public static function createWithTripId($attr, $id) { $attr['trip_id'] = $id; return TripEntry::create($attr); }
/** * returns JSON of requested entry * * @param $trip_id * @param $entry_id * @return mixed */ public function getMarker($trip_id, $entry_id) { $entry = TripEntry::findOrFail($entry_id); return $entry; }
/** * Remove the specified resource from storage. * * @param $trip_id * @param $entry_id * @param $pic_id * @return Response * @internal param int $id */ public function destroy($trip_id, $entry_id, $pic_id) { $pic = Picture::findOrFail($pic_id); $trip = Trip::findOrFail($trip_id); $entry = TripEntry::findOrFail($entry_id); $user = Auth::user(); if (Auth::user()->id == $trip->user_id && $trip->id == $entry->trip_id && $pic->trip_entry_id == $entry->id) { $pic->deleteWithImage($user->id); return redirect(url('trip') . '/' . $trip_id . '/entry/' . $entry->id); } else { return 'You are not authorized to delete this picture!'; } }