public function addTags($lead_id, Request $tags) { foreach ($tags as $tag) { $item = Tag::firstOrNew(['name' => $tag, 'lead_id' => $lead_id]); } return \Response::make('OK', 200); }
public function postEdit(Request $request) { $userID = Auth::user()->id; //Make sure title and tags are provided $validator = $this->editValidator($request->all()); if ($validator->fails()) { $this->throwValidationException($request, $validator); } $tags = explode(",", $request->tags); try { //Find the bookmark with ID=$id $bookmark = Bookmark::findOrFail($request->id); if ($bookmark->owner_id != $userID) { return "This is not your bookmark"; } //Update the bookmark's title $bookmark->title = $request->page_title; $bookmark->colour = $request->colour; $bookmark->save(); //Now create a new tag object for each of the tags //Only if it doesn't already exist in tags foreach ($tags as $tag_name) { // Retrieve the tag by the attributes, or instantiate a new instance... $tag = Tag::firstOrNew(array('tag_name' => $tag_name)); $tag->save(); $bookmarkTag = BookmarkTag::firstOrNew(array('bookmark_id' => $bookmark->bookmark_id, 'tag_id' => $tag->tag_id)); $bookmarkTag->save(); } } catch (ModelNotFoundException $e) { return "Bookmark not found: " . $e->getMessage(); } return redirect($this->bookmarksPath); }
/** * Update the specified resource in storage. * * @param Request $request * @param int $id * @return Response */ public function update(Request $request, $id) { $token = $request->header('token'); $user = $this->api ? User::where('token', $token)->first() : Auth::user(); $userID = isset($user) ? $user->id : 0; if (!$userID) { return $this->api ? response()->json(['data' => ['message' => 'Not logged in']], 401) : redirect(route('login')); } $post = PostBlog::whereRaw('id = ? AND userID = ?', [$id, $userID])->first(); $fields = $request->all(); $validator = Validator::make($fields, ['name' => 'required', 'description' => 'required', 'text' => 'required']); if ($validator->fails()) { if (!$this->api) { return redirect(route('edit', ['id' => $id]))->withErrors($validator); } else { return response()->json(['data' => ['errors' => $validator->errors()->getMessages()]], 400); } } $post->userID = $userID; $post->name = $fields['name']; $post->description = $fields['description']; $post->text = $fields['text']; $post->active = isset($fields['active']) ? 1 : 0; $post->save(); $tags = explode(',', $fields['tags']); foreach ($tags as $tag) { $tagObj = Tag::firstOrNew(['tagName' => $tag]); $tagObj->save(); PostBlogTag::firstOrNew(['postID' => $post->id, 'tagID' => $tagObj->id])->save(); } return $this->api ? response()->json(['data' => ['message' => 'Edit successfully']], 200) : redirect(route('home')); }