/** * Comments list * * @param $request * @param $matches * @return mixed */ public function likes_list($request, $matches) { $post = trim($matches->get('post'), '-+ '); $filter = ['conditions' => ['post = ?', $post], 'order' => 'created_at DESC']; /** @var Listing $paginator */ $paginator = NCService::load('Paginator.Listing', [$request->page, \Like::count($filter)]); $filter = array_merge($filter, $paginator->limit()); // Likes $likes = \Like::find('all', $filter); $likes = \Like::as_array($likes); // Context $context = ['rating' => \Like::rating($post), 'likes' => $likes, 'page' => $paginator->cur_page, 'listing' => $paginator->pages()]; // Build response if ($request->get('type', 'html') == 'json') { unset($context['listing']); $context['pages'] = $paginator->pages; $context['rows'] = $paginator->num_rows; return static::json_response($context); } return $this->view->render('likes/list.twig', $context); }
public function like(Request $request, Post $post) { # Get Login User $user = Sentinel::getUser(); if ($post->isLiked($user) > 0) { # When User Liked this Post $isdeleted = $post->likes()->where('user_id', $user->id)->delete(); if ($isdeleted) { $totalpostlikes = $post->likes()->count(); $totallikes = Like::count(); if ($request->ajax()) { return response()->json(['result' => true, 'status' => 'unlike', 'totalpostlikes' => $totalpostlikes, 'totallikes' => $totallikes]); } else { return redirect()->route('post.show', ['post' => $post->id])->with('success', 'درخواست شما با موفقیت انجام شد.'); } } } else { # When User didn't Like this Post before $like = new Like(); $like->user_id = $user->id; if ($post->likes()->save($like)) { $totalpostlikes = $post->likes()->count(); $totallikes = Like::count(); if ($request->ajax()) { return response()->json(['result' => true, 'status' => 'like', 'totalpostlikes' => $totalpostlikes, 'totallikes' => $totallikes]); } else { return redirect()->route('post.show', ['post' => $post->id])->with('success', 'پست با موفقیت لایک شد.'); } } } if ($request->ajax()) { return response()->json(['result' => false]); } else { return redirect()->home()->with('fail', 'مشکل در اتصال به سرور. لطفا مجددا تلاش کنید.'); } }