SearchController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Search;
  5. use App\Models\Product;
  6. use App\Models\Category;
  7. use App\Models\FlashDeal;
  8. use App\Models\Brand;
  9. use App\Models\Color;
  10. use App\Models\Shop;
  11. use App\Models\Attribute;
  12. use App\Models\AttributeCategory;
  13. use App\Utility\CategoryUtility;
  14. class SearchController extends Controller
  15. {
  16. public function index(Request $request, $category_id = null, $brand_id = null)
  17. {
  18. $query = $request->keyword;
  19. $sort_by = $request->sort_by;
  20. $min_price = $request->min_price;
  21. $max_price = $request->max_price;
  22. $seller_id = $request->seller_id;
  23. $attributes = Attribute::all();
  24. $selected_attribute_values = array();
  25. $colors = Color::all();
  26. $selected_color = null;
  27. $conditions = ['p.published' => 1];
  28. if ($brand_id != null) {
  29. $conditions = array_merge($conditions, ['p.brand_id' => $brand_id]);
  30. } elseif ($request->brand != null) {
  31. $brand_id = (Brand::where('slug', $request->brand)->first() != null) ? Brand::where('slug', $request->brand)->first()->id : null;
  32. $conditions = array_merge($conditions, ['p.brand_id' => $brand_id]);
  33. }
  34. // if ($seller_id != null) {
  35. // $conditions = array_merge($conditions, ['user_id' => Seller::findOrFail($seller_id)->user->id]);
  36. // }
  37. $products = Product::query()->from("products as p")->where($conditions);
  38. if ($category_id != null) {
  39. $category_ids = CategoryUtility::children_ids($category_id);
  40. $category_ids[] = $category_id;
  41. $products->whereIn('p.category_id', $category_ids);
  42. $attribute_ids = AttributeCategory::whereIn('category_id', $category_ids)->pluck('attribute_id')->toArray();
  43. $attributes = Attribute::whereIn('id', $attribute_ids)->get();
  44. } else {
  45. // if ($query != null) {
  46. // foreach (explode(' ', trim($query)) as $word) {
  47. // $ids = Category::where('name', 'like', '%'.$word.'%')->pluck('id')->toArray();
  48. // if (count($ids) > 0) {
  49. // foreach ($ids as $id) {
  50. // $category_ids[] = $id;
  51. // array_merge($category_ids, CategoryUtility::children_ids($id));
  52. // }
  53. // }
  54. // }
  55. // $attribute_ids = AttributeCategory::whereIn('category_id', $category_ids)->pluck('attribute_id')->toArray();
  56. // $attributes = Attribute::whereIn('id', $attribute_ids)->get();
  57. // }
  58. }
  59. if ($min_price != null && $max_price != null) {
  60. $products->where('p.unit_price', '>=', $min_price)->where('unit_price', '<=', $max_price);
  61. }
  62. if ($query != null) {
  63. $searchController = new SearchController;
  64. $searchController->store($request);
  65. $products->join("product_stocks as ps","p.id","=","ps.product_id");
  66. $products->join("product_translations as pt","p.id","=","pt.product_id");
  67. $products->where(function ($q) use ($query) {
  68. foreach (explode(' ', trim($query)) as $word) {
  69. $q->where('p.name', 'like', '%' . $word . '%')
  70. ->orWhere('tags', 'like', '%' . $word . '%')
  71. ->where('pt.name', 'like', '%' . $word . '%')
  72. ->where('ps.sku', 'like', '%' . $word . '%');
  73. }
  74. });
  75. }
  76. switch ($sort_by) {
  77. case 'newest':
  78. $products->orderBy('p.created_at', 'desc');
  79. break;
  80. case 'oldest':
  81. $products->orderBy('p.created_at', 'asc');
  82. break;
  83. case 'price-asc':
  84. $products->orderBy('p.unit_price', 'asc');
  85. break;
  86. case 'price-desc':
  87. $products->orderBy('p.unit_price', 'desc');
  88. break;
  89. default:
  90. $products->orderBy('p.id', 'desc');
  91. break;
  92. }
  93. if ($request->has('color')) {
  94. $str = '"' . $request->color . '"';
  95. $products->where('colors', 'like', '%' . $str . '%');
  96. $selected_color = $request->color;
  97. }
  98. if ($request->has('selected_attribute_values')) {
  99. $selected_attribute_values = $request->selected_attribute_values;
  100. foreach ($selected_attribute_values as $key => $value) {
  101. $str = '"' . $value . '"';
  102. $products->where('p.choice_options', 'like', '%' . $str . '%');
  103. }
  104. }
  105. $products = filter_products($products)->with('taxes')->paginate(12)->appends(request()->query());
  106. return view('frontend.product_listing', compact('products', 'query', 'category_id', 'brand_id', 'sort_by', 'seller_id', 'min_price', 'max_price', 'attributes', 'selected_attribute_values', 'colors', 'selected_color'));
  107. }
  108. public function listing(Request $request)
  109. {
  110. return $this->index($request);
  111. }
  112. public function listingByCategory(Request $request, $category_slug)
  113. {
  114. $category = Category::where('slug', $category_slug)->first();
  115. if ($category != null) {
  116. return $this->index($request, $category->id);
  117. }
  118. abort(404);
  119. }
  120. public function listingByBrand(Request $request, $brand_slug)
  121. {
  122. $brand = Brand::where('slug', $brand_slug)->first();
  123. if ($brand != null) {
  124. return $this->index($request, null, $brand->id);
  125. }
  126. abort(404);
  127. }
  128. //Suggestional Search
  129. public function ajax_search(Request $request)
  130. {
  131. $keywords = array();
  132. $query = $request->search;
  133. $products = Product::where('published', 1)->where('tags', 'like', '%' . $query . '%')->get();
  134. foreach ($products as $key => $product) {
  135. foreach (explode(',', $product->tags) as $key => $tag) {
  136. if (stripos($tag, $query) !== false) {
  137. if (sizeof($keywords) > 5) {
  138. break;
  139. } else {
  140. if (!in_array(strtolower($tag), $keywords)) {
  141. array_push($keywords, strtolower($tag));
  142. }
  143. }
  144. }
  145. }
  146. }
  147. $products = filter_products(Product::query()->from("products as p"));
  148. $products->join("product_translations as pt","p.id","=","pt.product_id");
  149. $products->join("product_stocks as ps","p.id","=","ps.product_id");
  150. $products=$products->where(function ($q) use ($query) {
  151. foreach (explode(' ', trim($query)) as $word) {
  152. $q->where('p.name', 'like', '%' . $word . '%')
  153. ->orWhere('tags', 'like', '%' . $word . '%')
  154. ->where('pt.name', 'like', '%' . $word . '%')
  155. ->where('ps.sku', 'like', '%' . $word . '%');
  156. }
  157. })
  158. // $products = $products->where('p.published', 1)
  159. // ->where(function ($q) use ($query) {
  160. // foreach (explode(' ', trim($query)) as $word) {
  161. // $q->where('name', 'like', '%' . $word . '%')
  162. // ->orWhere('tags', 'like', '%' . $word . '%')
  163. // ->orWhereHas('product_translations', function ($q) use ($word) {
  164. // $q->where('name', 'like', '%' . $word . '%');
  165. // })
  166. // ->orWhereHas('stocks', function ($q) use ($word) {
  167. // $q->where('sku', 'like', '%' . $word . '%');
  168. // });
  169. // }
  170. // })
  171. ->limit(3)
  172. ->get();
  173. $categories = Category::where('name', 'like', '%' . $query . '%')->get()->take(3);
  174. $shops = Shop::whereIn('user_id', verified_sellers_id())->where('name', 'like', '%' . $query . '%')->get()->take(3);
  175. // dump(sizeof($keywords));
  176. // dump(sizeof($categories));
  177. // dump(sizeof($products));
  178. // dump($keywords);
  179. // dump($categories);
  180. // dump($products);
  181. // dump($shops);
  182. if (sizeof($keywords) > 0 || sizeof($categories) > 0 || sizeof($products) > 0 || $shops->isNotEmpty()) {
  183. return view('frontend.partials.search_content', compact('products', 'categories', 'keywords', 'shops'));
  184. }
  185. return '0';
  186. }
  187. /**
  188. * Store a newly created resource in storage.
  189. *
  190. * @param \Illuminate\Http\Request $request
  191. * @return \Illuminate\Http\Response
  192. */
  193. public function store(Request $request)
  194. {
  195. $search = Search::where('query', $request->keyword)->first();
  196. if ($search != null) {
  197. $search->count = $search->count + 1;
  198. $search->save();
  199. } else {
  200. $search = new Search;
  201. $search->query = $request->keyword;
  202. $search->save();
  203. }
  204. }
  205. }