public function filterPosts(Request $request) { $query = $request->city_id ? 'city_id = ' . (int) $request->city_id . ' AND ' : NULL; $query .= $request->image == 'on' ? 'image IS NOT NULL AND ' : NULL; $query .= $request->from ? 'price >= ' . (int) $request->from . ' AND ' : 'price >= 0 AND '; $query .= $request->to ? 'price <= ' . (int) $request->to : 'price <= 9999999999'; if ($request->text) { $text = trim(strip_tags($request->text)); $query .= " AND (title LIKE '%{$text}%' or description LIKE '%{$text}%')"; } $section = Section::all(); $profiles = Profile::take(5)->get(); if ($request->category_id) { $section = Section::find($request->category_id); $posts = Post::where('status', 1)->where('category_id', $request->category_id)->whereRaw($query)->orderBy('id', 'DESC')->paginate(10); $posts->appends(['category_id' => (int) $request->category_id, 'city_id' => (int) $request->city_id, 'text' => $request->text, 'image' => $request->image == 'on' ? 'on' : NULL, 'from' => (int) $request->from, 'to' => (int) $request->to, 'tag_id' => $request->tags_id]); } else { $posts = Post::where('status', 1)->whereRaw($query)->orderBy('id', 'DESC')->paginate(10); $posts->appends(['city_id' => (int) $request->city_id, 'text' => $request->text, 'image' => $request->image == 'on' ? 'on' : NULL, 'from' => (int) $request->from, 'to' => (int) $request->to, 'tag_id' => $request->tags_id]); } $selected_tags = []; if (isset($request->tags_id)) { $selected_tags = $request->tags_id; foreach ($posts as $post_key => $post) { $hasTag = false; foreach ($selected_tags as $tag_id) { if ($post->hasTag($tag_id)) { $hasTag = true; } } if (!$hasTag) { unset($posts[$post_key]); } } } $category = Category::findOrFail($request->category_id); $category_tags = $category->tags()->get(); $favorites = ProfileController::getFavorites($request); $favorites = $favorites ? $favorites : []; return view('board.posts', compact('category', 'category_tags', 'selected_tags', 'section', 'sections', 'profiles', 'posts', 'favorites')); }
public function showMyFavorites(Request $request) { $favorites = $this->getFavorites($request); $favorites = $favorites ? $favorites : []; $profiles = Profile::take(5)->get(); $posts = Post::whereIn('id', array_values($favorites))->orderBy('id', 'DESC')->get(); return view('profile.my_favorites', compact('posts', 'favorites', 'profiles')); }
public function filterPosts(Request $request) { $query = $request->city_id ? 'city_id = ' . (int) $request->city_id . ' AND ' : NULL; $query .= $request->image == 'on' ? 'image IS NOT NULL AND ' : NULL; $query .= $request->from ? 'price >= ' . (int) $request->from . ' AND ' : 'price >= 0 AND '; $query .= $request->to ? 'price <= ' . (int) $request->to : 'price <= 9999999'; if ($request->text) { $text = trim(strip_tags($request->text)); $query .= " AND (title LIKE '%{$text}%' or description LIKE '%{$text}%')"; } $sections = Section::all(); $profiles = Profile::take(5)->get(); if ($request->section_id) { $section = Section::find($request->section_id); $posts = Post::where('status', 1)->where('section_id', $request->section_id)->whereRaw($query)->orderBy('id', 'DESC')->paginate(10); $posts->appends(['section_id' => (int) $request->section_id, 'city_id' => (int) $request->city_id, 'text' => $request->text, 'image' => $request->image == 'on' ? 'on' : NULL, 'from' => (int) $request->from, 'to' => (int) $request->to]); } else { $posts = Post::where('status', 1)->whereRaw($query)->orderBy('id', 'DESC')->paginate(10); $posts->appends(['city_id' => (int) $request->city_id, 'text' => $request->text, 'image' => $request->image == 'on' ? 'on' : NULL, 'from' => (int) $request->from, 'to' => (int) $request->to]); } return view('board.found_posts', compact('section', 'sections', 'profiles', 'posts')); }