Esempio n. 1
0
 public function link(Request $request)
 {
     // validation
     $this->validate($request, ['tag' => 'required|string|max:16']);
     // retrieve item for tagging
     $item = myCloset\Item::find($request->item_id);
     // Error checking for if the item already has this tag.
     $newTag = strtolower($request->tag);
     $tags = $item->tags;
     foreach ($tags as $tag) {
         if (strcmp($tag->name, $newTag) == 0) {
             \Session::flash('flash_message', 'This item already has this tag.');
             return redirect::to('/items/' . $request->item_id);
         }
     }
     // So as to actually reuse already created tags and save database space.
     $needle = strtolower($request->tag);
     $allTags = myCloset\Tag::lists('name')->toArray();
     if (in_array($needle, $allTags)) {
         // tag exists in the database, get it and save the relationship
         $tag = myCloset\Tag::where('name', $needle)->first();
     } else {
         // tag doesn't yet exist in the database.
         $tag = new myCloset\Tag();
         $tag->name = strtolower($request->tag);
         $tag->save();
     }
     // create the pivot table relationship
     $item->tags()->attach($tag);
     return redirect::to('/items/' . $request->item_id);
 }
Esempio n. 2
0
 public function run()
 {
     $tags = \myCloset\Tag::all();
     $items = \myCloset\Item::all();
     foreach ($items as $item) {
         for ($i = 0; $i < 4; $i++) {
             $tag = $tags->random();
             $item->tags()->save($tag);
         }
     }
 }