public function postLikepost() { $data = Input::all(); $rules = ["post_id" => 'required|exists:posts,id']; $validator = Validator::make($data, $rules); if ($validator->fails()) { return 404; } else { $check_like = Like::check_like(Auth::user()->id, $data['post_id']); if ($check_like) { $unlike = Like::unlike(Auth::user()->id, $data['post_id']); if ($unlike) { $total_like = Like::total_like($data['post_id']); $total_like_text = $total_like ? $total_like . " people like this" : ""; return json_encode(["like" => "Like", "total_like" => $total_like_text]); } else { return 404; } } else { $like = Like::like(Auth::user()->id, $data['post_id']); if ($like) { $total_like = Like::total_like($data['post_id']); $total_like_text = $total_like ? $total_like . " people like this" : ""; return json_encode(["like" => "Unlike", "total_like" => $total_like_text]); } else { return 404; } } } }
/** * Get all data of post * @author Tran Van Moi * @since 2015/05/12 * @return array */ public static function get_all_posts($category_id, $keyword = "", $user_id = false) { $posts = DB::table('posts'); $posts->join('users', 'posts.user_id', '=', 'users.id'); $posts->select('posts.id', 'posts.title', 'posts.content', 'posts.image as image_post', 'posts.created_at', 'posts.updated_at', 'users.image', 'users.name as username', 'users.id as user_id'); $posts->orderBy('created_at', 'desc'); $posts->orderBy('updated_at', 'desc'); if (is_numeric($category_id) && Category::find($category_id)) { $posts->where('posts.category_id', '=', $category_id); } if ($user_id) { $posts->where('posts.user_id', '=', $user_id); } $posts->where('posts.delete_status', '=', 0); if ($keyword != "") { $posts->where(function ($posts) use($keyword) { $posts->where('posts.title', 'like', '%' . $keyword . '%')->orWhere('posts.content', 'like', '%' . $keyword . '%')->orWhere('users.name', 'like', '%' . $keyword . '%'); }); } $posts = $posts->get(); foreach ($posts as $key => $value) { $path = "./public/images/post/"; if (File::exists($path . $value->image_post) && $value->image_post != "") { $posts[$key]->image_post = URL::to('/') . "/public/images/post/" . $value->image_post; } else { $posts[$key]->image_post = ""; } $now = gmdate("Y-m-d G:i:s", time() + 7 * 3600); $date1 = new DateTime($now); $date = $value->updated_at > $value->created_at ? $value->updated_at : $value->created_at; $date2 = new DateTime($date); $interval = $date1->diff($date2); if ($interval->d) { $posts[$key]->time = date("F j, Y, g:i a", strtotime($date)); } else { if ($interval->h) { $posts[$key]->time = $interval->h . " hrs"; } elseif ($interval->i) { $posts[$key]->time = $interval->i . " mins"; } else { $posts[$key]->time = "Just now"; } } unset($posts[$key]->created_at); unset($posts[$key]->updated_at); $url_image = ""; if (filter_var($value->image, FILTER_VALIDATE_URL) === false) { $posts[$key]->image = URL::to('/') . "/public/images/avatar/" . $value->image; } $posts[$key]->like = Like::check_like(Auth::user()->id, $value->id); $posts[$key]->total_like = Like::total_like($value->id); $posts[$key]->comment = \DB::table('comments')->join('users', 'comments.user_id', '=', 'users.id')->select('comments.id', 'comments.content', 'comments.created_at', 'comments.updated_at', 'users.name as username', 'users.id as user_id', 'users.image')->where('comments.post_id', '=', $value->id)->where('comments.delete_status', '=', 0)->orderBy('comments.created_at', 'asc')->orderBy('comments.updated_at', 'asc')->get(); } return $posts; }