123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Models\Search;
- use App\Models\Product;
- use App\Models\Category;
- use App\Models\FlashDeal;
- use App\Models\Brand;
- use App\Models\Color;
- use App\Models\Shop;
- use App\Models\Attribute;
- use App\Models\AttributeCategory;
- use App\Utility\CategoryUtility;
- class SearchController extends Controller
- {
- public function index(Request $request, $category_id = null, $brand_id = null)
- {
- $query = $request->keyword;
- $sort_by = $request->sort_by;
- $min_price = $request->min_price;
- $max_price = $request->max_price;
- $seller_id = $request->seller_id;
- $attributes = Attribute::all();
- $selected_attribute_values = array();
- $colors = Color::all();
- $selected_color = null;
- $conditions = ['p.published' => 1];
- if ($brand_id != null) {
- $conditions = array_merge($conditions, ['p.brand_id' => $brand_id]);
- } elseif ($request->brand != null) {
- $brand_id = (Brand::where('slug', $request->brand)->first() != null) ? Brand::where('slug', $request->brand)->first()->id : null;
- $conditions = array_merge($conditions, ['p.brand_id' => $brand_id]);
- }
- // if ($seller_id != null) {
- // $conditions = array_merge($conditions, ['user_id' => Seller::findOrFail($seller_id)->user->id]);
- // }
- $products = Product::query()->from("products as p")->where($conditions);
- if ($category_id != null) {
- $category_ids = CategoryUtility::children_ids($category_id);
- $category_ids[] = $category_id;
- $products->whereIn('p.category_id', $category_ids);
- $attribute_ids = AttributeCategory::whereIn('category_id', $category_ids)->pluck('attribute_id')->toArray();
- $attributes = Attribute::whereIn('id', $attribute_ids)->get();
- } else {
- // if ($query != null) {
- // foreach (explode(' ', trim($query)) as $word) {
- // $ids = Category::where('name', 'like', '%'.$word.'%')->pluck('id')->toArray();
- // if (count($ids) > 0) {
- // foreach ($ids as $id) {
- // $category_ids[] = $id;
- // array_merge($category_ids, CategoryUtility::children_ids($id));
- // }
- // }
- // }
- // $attribute_ids = AttributeCategory::whereIn('category_id', $category_ids)->pluck('attribute_id')->toArray();
- // $attributes = Attribute::whereIn('id', $attribute_ids)->get();
- // }
- }
- if ($min_price != null && $max_price != null) {
- $products->where('p.unit_price', '>=', $min_price)->where('unit_price', '<=', $max_price);
- }
- if ($query != null) {
- $searchController = new SearchController;
- $searchController->store($request);
- $products->join("product_stocks as ps","p.id","=","ps.product_id");
- $products->join("product_translations as pt","p.id","=","pt.product_id");
- $products->where(function ($q) use ($query) {
- foreach (explode(' ', trim($query)) as $word) {
- $q->where('p.name', 'like', '%' . $word . '%')
- ->orWhere('tags', 'like', '%' . $word . '%')
- ->where('pt.name', 'like', '%' . $word . '%')
- ->where('ps.sku', 'like', '%' . $word . '%');
- }
- });
- }
- switch ($sort_by) {
- case 'newest':
- $products->orderBy('p.created_at', 'desc');
- break;
- case 'oldest':
- $products->orderBy('p.created_at', 'asc');
- break;
- case 'price-asc':
- $products->orderBy('p.unit_price', 'asc');
- break;
- case 'price-desc':
- $products->orderBy('p.unit_price', 'desc');
- break;
- default:
- $products->orderBy('p.id', 'desc');
- break;
- }
- if ($request->has('color')) {
- $str = '"' . $request->color . '"';
- $products->where('colors', 'like', '%' . $str . '%');
- $selected_color = $request->color;
- }
- if ($request->has('selected_attribute_values')) {
- $selected_attribute_values = $request->selected_attribute_values;
- foreach ($selected_attribute_values as $key => $value) {
- $str = '"' . $value . '"';
- $products->where('p.choice_options', 'like', '%' . $str . '%');
- }
- }
- $products = filter_products($products)->with('taxes')->paginate(12)->appends(request()->query());
- 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'));
- }
- public function listing(Request $request)
- {
- return $this->index($request);
- }
- public function listingByCategory(Request $request, $category_slug)
- {
- $category = Category::where('slug', $category_slug)->first();
- if ($category != null) {
- return $this->index($request, $category->id);
- }
- abort(404);
- }
- public function listingByBrand(Request $request, $brand_slug)
- {
- $brand = Brand::where('slug', $brand_slug)->first();
- if ($brand != null) {
- return $this->index($request, null, $brand->id);
- }
- abort(404);
- }
- //Suggestional Search
- public function ajax_search(Request $request)
- {
- $keywords = array();
- $query = $request->search;
- $products = Product::where('published', 1)->where('tags', 'like', '%' . $query . '%')->get();
- foreach ($products as $key => $product) {
- foreach (explode(',', $product->tags) as $key => $tag) {
- if (stripos($tag, $query) !== false) {
- if (sizeof($keywords) > 5) {
- break;
- } else {
- if (!in_array(strtolower($tag), $keywords)) {
- array_push($keywords, strtolower($tag));
- }
- }
- }
- }
- }
- $products = filter_products(Product::query()->from("products as p"));
- $products->join("product_translations as pt","p.id","=","pt.product_id");
- $products->join("product_stocks as ps","p.id","=","ps.product_id");
- $products=$products->where(function ($q) use ($query) {
- foreach (explode(' ', trim($query)) as $word) {
- $q->where('p.name', 'like', '%' . $word . '%')
- ->orWhere('tags', 'like', '%' . $word . '%')
- ->where('pt.name', 'like', '%' . $word . '%')
- ->where('ps.sku', 'like', '%' . $word . '%');
- }
- })
- // $products = $products->where('p.published', 1)
- // ->where(function ($q) use ($query) {
- // foreach (explode(' ', trim($query)) as $word) {
- // $q->where('name', 'like', '%' . $word . '%')
- // ->orWhere('tags', 'like', '%' . $word . '%')
- // ->orWhereHas('product_translations', function ($q) use ($word) {
- // $q->where('name', 'like', '%' . $word . '%');
- // })
- // ->orWhereHas('stocks', function ($q) use ($word) {
- // $q->where('sku', 'like', '%' . $word . '%');
- // });
- // }
- // })
- ->limit(3)
- ->get();
- $categories = Category::where('name', 'like', '%' . $query . '%')->get()->take(3);
- $shops = Shop::whereIn('user_id', verified_sellers_id())->where('name', 'like', '%' . $query . '%')->get()->take(3);
- // dump(sizeof($keywords));
- // dump(sizeof($categories));
- // dump(sizeof($products));
- // dump($keywords);
- // dump($categories);
- // dump($products);
- // dump($shops);
- if (sizeof($keywords) > 0 || sizeof($categories) > 0 || sizeof($products) > 0 || $shops->isNotEmpty()) {
- return view('frontend.partials.search_content', compact('products', 'categories', 'keywords', 'shops'));
- }
- return '0';
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- $search = Search::where('query', $request->keyword)->first();
- if ($search != null) {
- $search->count = $search->count + 1;
- $search->save();
- } else {
- $search = new Search;
- $search->query = $request->keyword;
- $search->save();
- }
- }
- }
|