/** * Show the form required by an ajax request. * * @param $parent * @param FormBuilder $formBuilder * * @return \Illuminate\Http\Response */ public function add(FormBuilder $formBuilder, $parent) { $articleComment = ArticleComment::whereId($parent)->get(['article_id'])->first(); $slug = Article::whereId($articleComment->article_id)->get(['slug'])->first(); $form = $formBuilder->create('\\App\\Http\\Forms\\ArticleCommentForm', ['method' => 'POST', 'id' => 'article_comment_form', 'url' => route('articles.comment.store')]); $form->add('slug', 'hidden', ['value' => $slug->slug, 'attr' => ['id' => 'slug']]); $form->add('parent_id', 'hidden', ['value' => $parent, 'attr' => ['id' => 'parent_id']]); $form->add('save', 'submit', ['label' => 'Add your comment']); return response()->view('articles.ajax_article_comment_show', compact('form')); }
/** * ajax post like to article * * @param Request $request * @param int $article_id * @return Response */ public function ajaxPostLike(Request $request, $article_id) { // check authentication if (!auth()->check()) { return response()->json(['error' => 'user is not authenticated']); } // check the user has liked it or not // temp plus variable $plus = 1; //-- get article $article = Article::whereId($article_id)->first(); //-- get current user $user = auth()->user(); // user has liked, decrement num_of_likes if ($article->likeArticles()->whereUser_id($user->id)->first()) { $plus = -1; } else { event(new PushNotification($article->user, $user->nickname . ' 喜歡您的文章 : ' . $article->title)); } // determine to create LikeArticle or deleted liked record if ($plus == 1) { LikeArticle::create(['user_id' => $user->id, 'article_id' => $article_id]); } else { LikeArticle::whereUser_id($user->id)->whereArticle_id($article_id)->first()->delete(); } // increment number of likes $article->update(['num_of_likes' => $article->num_of_likes + $plus]); return response()->json(['num_of_likes' => $article->num_of_likes]); }
/** * Authorize the upload action of a picture album * see the route list for details * * @param $model * @param $model_id * @param $id * * @return bool */ private function _authorize($model, $model_id, $id) { switch ($model) { case 'user': if ($id != 'new') { $this->album = Album::where('id', $id)->first(); session(['album_id' => null]); } else { if (session('album_id') && session('album_id') != null) { $this->album = Album::where('id', session('album_id'))->first(); } else { $this->makeNewAlbum(); Auth::user()->albums()->save($this->album); $this->album->update(['user_id' => Auth::id()]); } } break; case 'article': $this->article = Article::whereId($id)->first(); if (!($this->album = Album::where('albumable_id', $id)->get()->first())) { $this->makeNewAlbum(); $this->article->albums()->save($this->album); } break; case 'profession': $this->profession = Profession::whereId($id)->first(); if (!($this->album = Album::where('albumable_id', $id)->get()->first())) { $this->makeNewAlbum(); $this->profession->albums()->save($this->album); } break; case 'site': $this->site = Site::whereId($model_id)->first(); if (!($this->album = Album::where('id', $id)->first())) { if (!$this->site->albums->last()) { $this->makeNewAlbum(); $this->site->albums()->save($this->site); } } break; case 'classified': $this->classified = Classified::whereId($id)->first(); if (!($this->classified = Album::where('albumable_id', $id)->get()->first())) { $this->makeNewAlbum(); $this->classified->albums()->save($this->album); } break; } //session(['album_id' => $this->album->id]); return true; }