/** * Responds to requests to POST /photos/{id}/create */ public function postCreate(Request $request, $id = null) { $landmark = \App\Landmark::find($id); if (is_null($landmark)) { \Session::flash('flash_message', 'Landmark not found.'); return redirect('\\landmarks'); } if (\App\Photo::where('filepath', '=', $request->filepath)->exists()) { \Session::flash('flash_message', 'A Photo with that URL already exists in our database.'); return redirect('/photos/' . $id); } $this->validate($request, ['filepath' => 'required|url', 'photo_description' => 'required|min:1']); # Enter photo $photo = new \App\Photo(); $photo->landmark()->associate($landmark->id); $photo->user()->associate(\Auth::id()); # <--- NEW LINE $photo->filepath = $request->filepath; $photo->photo_description = $request->photo_description; $photo->save(); # Done \Session::flash('flash_message', 'Your photo was added!'); return redirect('/photos/' . $id); }