Esempio n. 1
0
 /**
  * Responds to requests to POST /landmark/create
  */
 public function postCreate(Request $request)
 {
     if (\App\Landmark::where('name', '=', $request->name)->exists()) {
         \Session::flash('flash_message', 'A Landmark with that name already exists.');
         return redirect('\\landmarks');
     }
     $this->validate($request, ['name' => 'required|min:5', 'description' => 'required|min:1', 'location' => 'required|min:1', 'filepath' => 'required|url', 'photo_description' => 'required|min:1']);
     # Enter landmark and its photo into the database
     $landmark = new \App\Landmark();
     $landmark->name = $request->name;
     $landmark->description = $request->description;
     $landmark->location = $request->location;
     $landmark->user_id = \Auth::id();
     # <--- NEW LINE
     $landmark->save();
     $photo = new \App\Photo();
     $photo->filepath = $request->filepath;
     $photo->photo_description = $request->photo_description;
     $photo->landmark_id = $landmark->id;
     $photo->user_id = \Auth::id();
     $photo->save();
     # Add the tags
     $request->tags;
     if ($request->tags) {
         $tags = $request->tags;
     } else {
         $tags = [];
     }
     $landmark->tags()->sync($tags);
     # Done
     \Session::flash('flash_message', 'Your landmark was added!');
     return redirect('/landmarks');
 }
Esempio n. 2
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     # First, create an array of all the landmarks we want to associate tags with
     # The *key* will be the landmark id, and the *value* will be an array of tags.
     $landmarks = ['1' => ['bridge', 'metallic', 'large', 'scenic'], '2' => ['ethnic', 'Chinese', 'gate', 'large'], '3' => ['statue', 'Harvard', 'academic', 'metallic']];
     # Now loop through the above array, creating a new pivot for each book to tag
     foreach ($landmarks as $landmark => $tags) {
         # First get the landmark
         $landmark = \App\Landmark::where('id', 'like', $landmark)->first();
         # Now loop through each tag for this landmark, adding the pivot
         foreach ($tags as $tagName) {
             $tag = \App\Tag::where('tag', 'LIKE', $tagName)->first();
             # Connect this tag to this book
             $landmark->tags()->save($tag);
         }
     }
 }