category indexes

This commit is contained in:
merdan 2022-05-03 16:01:43 +05:00
parent 13d58c6576
commit 9c217c4064
5 changed files with 115 additions and 53 deletions

View File

@ -117,57 +117,6 @@ class Products extends ProductController
}
public function suggestions(BrandRepository $brandRepository){
$key = request('search');
if(!strlen($key)>= 3){
return response()->json(['message' => '3 karakterden kuchuk','status'=>false]);
}
$queries = explode(' ', $key);
$brands = $brandRepository->getModel()::search(implode(' OR ', $queries))
// ->where('status',1)
// ->orderBy('name','asc')
->take(10)
->query(fn ($query) => $query->select('id','name')->orderBy('name'))
->get();
if($brands->count()){
$brands->flatMap(fn ($val) => $val['suggestion_type']='brand');
}
// $products = $this->productRepository->searchProductByAttribute($key);
$channel = core()->getRequestedChannelCode();
$locale = core()->getRequestedLocaleCode();
$products = app(ProductFlatRepository::class)->getModel()::search(implode(' OR ', $queries))
->where('status', 1)
->where('visible_individually', 1)
->where('channel', $channel)
->where('locale', $locale)
->take(10)
->query(fn ($query) => $query->select('id','name','product_id')
// ->addSelect(DB::raw("\'product\' as type" ))
->orderBy('name'))
->get();
if($products->count()){
$products->map(function ($item,$key) {
$item['suggestion_type'] = 'product';
return $item;
});
}
return Suggestion::collection($products->merge($brands)->sortBy('name'));
}
public function searchProducts(){
// return $this->productRepository->searchProductByAttribute(request('key'));
return ProductResource::collection($this->productRepository->searchProductByAttribute(request('key')));

View File

@ -0,0 +1,94 @@
<?php
namespace Sarga\API\Http\Controllers;
use App\Http\Controllers\Controller;
use Sarga\API\Http\Resources\Catalog\Suggestion;
use Sarga\Brand\Repositories\BrandRepository;
use Sarga\Shop\Repositories\CategoryRepository;
use Webkul\Product\Repositories\ProductFlatRepository;
class SearchController extends Controller
{
public function __construct(protected BrandRepository $brandRepository,
protected ProductFlatRepository $productFlatRepository,
protected CategoryRepository $categoryRepository)
{
}
public function index(){
$key = request('search');
if(!strlen($key)>= 3){
return response()->json(['message' => '3 karakterden kuchuk','status'=>false]);
}
$queries = explode(' ', $key);
$brands =$this->searchBrands($queries);
$products = $this->searchProducts($queries);
$categories = $this->searchCategories($queries);
return Suggestion::collection($products->merge($brands)->merge($categories)->sortBy('name'));
}
private function searchBrands($key){
$brands = $this->brandRepository->getModel()::search(implode(' OR ', $key))
// ->where('status',1)
// ->orderBy('name','asc')
->take(10)
->query(fn ($query) => $query->select('id','name')->orderBy('name'))
->get();
if($brands->count()){
$brands->flatMap(fn ($val) => $val['suggestion_type']='brand');
}
return $brands;
}
private function searchCategories($key){
$categories = $this->CategoryRepository->getModel()::search(implode(' OR ', $key))
->take(10)
->query(fn ($query) => $query->select('id','name')->orderBy('name'))
->get();
if($categories->count()){
$categories->flatMap(fn ($val) => $val['suggestion_type']='category');
}
return $categories;
}
private function searchProducts($key){
$channel = core()->getRequestedChannelCode();
$locale = core()->getRequestedLocaleCode();
$products = $this->productRepository->getModel()::search(implode(' OR ', $key))
->where('status', 1)
->where('visible_individually', 1)
->where('channel', $channel)
->where('locale', $locale)
->take(10)
->query(fn ($query) => $query->select('id','name','product_id')
// ->addSelect(DB::raw("\'product\' as type" ))
->orderBy('name'))
->get();
if($products->count()){
$products->map(function ($item,$key) {
$item['suggestion_type'] = 'product';
return $item;
});
}
return $products;
}
}

View File

@ -55,7 +55,7 @@ Route::group(['prefix' => 'api'], function () {
Route::get('products-discounted', [Products::class, 'discountedProducts']);
Route::get('products-popular', [Products::class, 'popularProducts']);
Route::get('products-search', [Products::class, 'searchProducts']);
Route::get('suggestions', [Products::class, 'suggestions']);
Route::get('suggestions', [\Sarga\API\Http\Controllers\SearchController::class, 'index']);
Route::get('products/{id}', [Products::class, 'get']);
Route::get('products/{id}/variants', [Products::class, 'variants']);

View File

@ -4,11 +4,13 @@ namespace Sarga\Shop\Models;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Laravel\Scout\Searchable;
use Sarga\Brand\Models\BrandProxy;
use Webkul\Category\Database\Factories\CategoryFactory;
use Webkul\Category\Models\Category as WCategory;
class Category extends WCategory
{
use Searchable;
/**
* Fillables.
*
@ -39,6 +41,24 @@ class Category extends WCategory
* @var array
*/
protected $appends = ['image_url', 'category_icon_url'];
public function searchableAs()
{
return 'category_index';
}
public function toSearchableArray()
{
return [
'id' => $this->id,
'name' => $this->name,
'type' => 'category'
];
}
public function shouldBeSearchable()
{
return $this->status;
}
/**
* Create a new factory instance for the model.

View File

@ -72,7 +72,6 @@ class CategoryRepository extends Repository
Event::dispatch('catalog.category.update.before', $id);
Log::info($data);
$data = $this->setSameAttributeValueToAllLocale($data, 'slug');
$category->update($data);