topic->slug}.{$post->topic->id}?page={$post->getPageNumber()}#post-{$postId}"; $user = $request->user(); $like = $user->likes()->where('post_id', '=', $post->id)->where('like', '=', 1)->first(); $dislike = $user->likes()->where('post_id', '=', $post->id)->where('dislike', '=', 1)->first(); if ($like || $dislike) { return redirect()->to($postUrl) ->withErrors('You have already liked/disliked this post!'); } elseif ($user->id == $post->user_id) { return redirect()->to($postUrl) ->withErrors('You cannot like your own post!'); } else { $new = new Like(); $new->user_id = $user->id; $new->post_id = $post->id; $new->like = 1; $new->save(); return redirect()->to($postUrl) ->withSuccess('Like Successfully Applied!'); } } /** * Dislike A Post. * * @param $postId * * @return Illuminate\Http\RedirectResponse */ public function destroy(Request $request, $postId) { $post = Post::findOrFail($postId); $postUrl = "forums/topic/{$post->topic->slug}.{$post->topic->id}?page={$post->getPageNumber()}#post-{$postId}"; $user = $request->user(); $like = $user->likes()->where('post_id', '=', $post->id)->where('like', '=', 1)->first(); $dislike = $user->likes()->where('post_id', '=', $post->id)->where('dislike', '=', 1)->first(); if ($like || $dislike) { return redirect()->to($postUrl) ->withErrors('You have already liked/disliked this post!'); } elseif ($user->id == $post->user_id) { return redirect()->to($postUrl) ->withErrors('You cannot dislike your own post!'); } else { $new = new Like(); $new->user_id = $user->id; $new->post_id = $post->id; $new->dislike = 1; $new->save(); return redirect()->to($postUrl) ->withSuccess('Dislike Successfully Applied!'); } } }