/** * User liked a post * @param Post $post * @return Redirect */ public function like(Post $post) { $alreadyLiked = $post->likesFor($this->auth->user()); $message = ''; if (!!$alreadyLiked->count()) { $message = 'You unliked a post.'; $alreadyLiked->delete(); } else { $message = 'You liked a post.'; $post->likes()->create(['user_id' => $this->auth->user()->id]); } return back()->withSuccessMessage($message); }
/** * Store a comment in a posts * @param $request * @param $postid * @return */ public function store(CommentsRequest $request, $postid) { $post = Post::with('owner')->findOrFail($request->post_id); $user = Auth::user(); $comment = new Comment($request->all()); $comment->attachTo($user)->attachTo($post)->save(); event(new CommentWasPosted($comment)); return redirect()->route('users.posts', [$post->owner->employee_id, $post->id])->with('success_message', 'Your comment has been posted.'); }
/** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { $post = Post::with('owner')->findOrFail($this->post_id); return $post->user_id == Auth::id() || Auth::user()->isFriendsWith($post->owner->id); }