Created
July 5, 2018 07:50
-
-
Save saaiful/dbf3728c6942df25faebea418338c949 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $vuetables = new EloquentVueTables; | |
| $model = new Product; | |
| $model = $model->where('status', 1); | |
| $filter = []; | |
| if ($request->has('q') && $request->q != "undefined") { | |
| $this->q = '%' . $request->q . '%'; | |
| // Named Sort | |
| $model = $model->where(function ($query) { | |
| $query->where('name', 'LIKE', $this->q) | |
| ->orWhere('short_description', 'LIKE', $this->q) | |
| ->orWhere('description', 'LIKE', $this->q) | |
| ->orWhere('keywords', 'LIKE', $this->q); | |
| }); | |
| /*$model1 = $model->where('name', 'LIKE', $this->q)->count('id'); | |
| if ($model1 >= $request->limit) { | |
| $model = $model->where('name', 'LIKE', $this->q); | |
| } else { | |
| $model = $model->where(function ($query) { | |
| $query->Where('short_description', 'LIKE', $this->q) | |
| ->orWhere('description', 'LIKE', $this->q) | |
| ->orWhere('keywords', 'LIKE', $this->q); | |
| }); | |
| }*/ | |
| } | |
| if ($request->has('cat')) { | |
| $model = $model->where('category_id', $request->cat); | |
| } | |
| if ($request->has('shop')) { | |
| $model = $model->where('shop_id', $request->shop); | |
| } else { | |
| $filter = Product::distinct()->get(['category_id'])->pluck('category_id'); | |
| $filter = Category::whereIn('id', $filter)->get(['id', 'en_name', 'bn_name']); | |
| } | |
| if ($request->has('price_range')) { | |
| $model = $model->whereBetween('sale', $request->price_range); | |
| } | |
| if ($request->has('promotion')) { | |
| // $model = $model->whereBetween('sale', $request->price_range); | |
| } | |
| if ($request->has('catagory')) { | |
| $model = $model->whereIn('category_id', $request->catagory); | |
| } | |
| if ($request->has('promotion')) { | |
| if (in_array("With Discount", $request->promotion)) { | |
| $model = $model->whereRaw('rate != sale'); | |
| } | |
| } | |
| if ($request->has('features')) { | |
| if (in_array("Whole Sale", $request->features)) { | |
| $model = $model->where('has_wholesale', 1); | |
| } | |
| } | |
| if ($request->has('sort')) { | |
| $sort = $request->sort; | |
| if ($sort == 'popular') { | |
| $model = $model->orderBy('sold', 'dsec'); | |
| } elseif ($sort == 'latest') { | |
| $model = $model->orderBy('id', 'dsec'); | |
| } elseif ($sort == 'top-sales') { | |
| $model = $model->orderBy('sold', 'dsec'); | |
| } elseif ($sort == 'on-sale') { | |
| $model = $model->whereRaw('rate != sale'); | |
| } elseif ($sort == 'price-up') { | |
| $model = $model->orderBy('sale', 'dsec'); | |
| } elseif ($sort == 'price-down') { | |
| $model = $model->orderBy('sale', 'asc'); | |
| } | |
| } | |
| $model = $model->select(['id', "category_id", "love", "sku", "shop_id", "name", "short_description", "description", "keywords", "images", "video", "rate", "sale", "has_stock", "stock", "rating", "ratings", "comments", "reviews", "sold", "review_point", "status", "attributes", "others", "weight", "shipping", "condition"]); | |
| $data = $model->paginate($request->get('limit', 12))->toArray(); | |
| $data['filters'] = $filter; | |
| $data['max_price'] = Product::max('sale'); | |
| return success($data, 200); |
Author
Author
Issue Resolved Using RawOrder
$model->orderByRaw("CASE WHEN instr(name, '" . $request->q . "') = 0 then 1 else 0 end, instr(name, '" . $request->q . "') desc");
Author
Finalized Using:
$model->orderByRaw("CASE WHEN instr(name, '" . escape($request->q) . "') = 0 then 1 else 0 end, instr(name, '" . escape($request->q) . "') desc");
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$model = $model->where(function ($query) { $query->where('name', 'LIKE', $this->q) ->orWhere('short_description', 'LIKE', $this->q) ->orWhere('description', 'LIKE', $this->q) ->orWhere('keywords', 'LIKE', $this->q); });This is the search part, which uses LIKE for searching. Though I implemented a searching system with Scout & T&T