예제 #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);
 }
예제 #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);
         }
     }
 }
예제 #3
0
 /**
  * Method: DELETE
  *
  * Deletes an item record from the database, removes tag associations, and
  * removes the item from disk.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     // get the item to be deleted and confirm it exists.
     $item = myCloset\Item::find($id);
     if (is_null($item)) {
         Session::flash('flash_message', 'Item not found.');
         return redirect('/items');
     }
     // delete the pivot table assocation
     if ($item->tags()) {
         $item->tags()->detach();
     }
     // Delete the item from database
     myCloset\Item::destroy($id);
     // delete the slash at the start of the $item->src
     $pathToDelete = substr($item->src, 1);
     // delete the item from disk
     if (file_exists($pathToDelete)) {
         unlink($pathToDelete);
     } else {
         // User doesn't need to know that we couldn't delete the file but
         // we should so let's log it.
         \Log::info('Could not delete file:' . $pathToDelete);
     }
     Session::flash('flash_message', 'Your item was successfully deleted.');
     return Redirect::to('/items');
 }