Compare commits

...

2 Commits

Author SHA1 Message Date
Mekan1206
bac2ad9a3e WIP 2026-05-03 18:42:16 +05:00
Mekan1206
1b467108de WIP 2026-05-03 18:36:49 +05:00

View File

@@ -6,6 +6,7 @@ use App\Http\Controllers\Api\V1\Filters\Requests\FilterIndexRequest;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Ecommerce\Channel\Channel; use App\Models\Ecommerce\Channel\Channel;
use App\Models\Ecommerce\Product\Brand\Brand; use App\Models\Ecommerce\Product\Brand\Brand;
use Illuminate\Support\Facades\DB;
use App\Models\Ecommerce\Product\Category\Category; use App\Models\Ecommerce\Product\Category\Category;
use App\Models\Ecommerce\Product\Collection\Collection; use App\Models\Ecommerce\Product\Collection\Collection;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
@@ -45,7 +46,7 @@ class FilterController extends Controller
} }
if ($this->shouldFilterByCollection()) { if ($this->shouldFilterByCollection()) {
return $this->filterByCategoryResource(Collection::find($this->request->collection_id)); return $this->categoriesFor();
} }
if ($this->shouldFilterByBrand()) { if ($this->shouldFilterByBrand()) {
@@ -62,29 +63,29 @@ class FilterController extends Controller
private function brands() private function brands()
{ {
if ($this->shouldFilterByBrand()) { if ($this->shouldFilterByBrand()) {
return Brand::query()->where('id', $this->request->brand_id)->get(['id', 'name']); return Brand::where('id', $this->request->brand_id)->get(['id', 'name']);
} }
if ($this->shouldFilterByCategory()) { if ($this->shouldFilterByCategory()) {
$brands = Category::query()->find($this->request->category_id)->products() $brands = Category::find($this->request->category_id)->products()
->where('products.is_visible', true) ->where('products.is_visible', true)
->where('products.parent_id', null) ->where('products.parent_id', null)
->where('products.stock', '>', 0) ->where('products.stock', '>', 0)
->distinct('products.brand_id') ->distinct('products.brand_id')
->pluck('products.brand_id'); ->pluck('products.brand_id');
return Brand::query()->whereIntegerInRaw('id', $brands)->get(['id', 'name']); return Brand::whereIntegerInRaw('id', $brands)->get(['id', 'name']);
} }
if ($this->shouldFilterByCollection()) { if ($this->shouldFilterByCollection()) {
$brands = Collection::query()->find($this->request->collection_id)->products() $brands = Collection::find($this->request->collection_id)->products()
->where('products.is_visible', true) ->where('products.is_visible', true)
->where('products.parent_id', null) ->where('products.parent_id', null)
->where('products.stock', '>', 0) ->where('products.stock', '>', 0)
->distinct('products.brand_id') ->distinct('products.brand_id')
->pluck('products.brand_id'); ->pluck('products.brand_id');
return Brand::query()->whereIntegerInRaw('id', $brands)->get(['id', 'name']); return Brand::whereIntegerInRaw('id', $brands)->get(['id', 'name']);
} }
return Brand::query()->where('is_visible', true)->ordered()->get(['id', 'name']); return Brand::query()->where('is_visible', true)->ordered()->get(['id', 'name']);
@@ -102,12 +103,36 @@ class FilterController extends Controller
->distinct('products.id') ->distinct('products.id')
->pluck('products.id'); ->pluck('products.id');
return Category::query() return Category::where('is_visible', true)
->distinct('categories.id') ->ordered()
->where('is_visible', true) ->join('product_has_relations', 'categories.id', '=', 'product_has_relations.productable_id')
->join('product_has_relations', 'categories.id', '=', 'product_has_relations.productable_id') ->where('product_has_relations.productable_type', '=', 'category')
->where('product_has_relations.productable_type', '=', 'category') ->whereIntegerInRaw('product_has_relations.product_id', $products)
->whereIntegerInRaw('product_has_relations.product_id', $products) ->get(['categories.id', 'categories.parent_id', 'categories.name'])
->unique('categories.id');
}
/**
* Categories for a resource
*/
private function categoriesFor($resource)
{
return DB::table('categories as c')
->select('c.id', 'c.parent_id', 'c.name')
->where('c.is_visible', true)
->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('product_has_relations as phr_cat')
->join('products as p', 'p.id', '=', 'phr_cat.product_id')
->join('product_has_relations as phr_chan', 'phr_chan.product_id', '=', 'p.id')
->whereColumn('phr_cat.productable_id', 'c.id')
->where('phr_cat.productable_type', 'category')
->where('phr_chan.productable_type', 'channel')
->where('phr_chan.productable_id', 6)
->where('p.is_visible', true)
->whereNull('p.parent_id')
->where('p.stock', '>', 0);
})
->get() ->get()
->map(fn ($category) => [ ->map(fn ($category) => [
'id' => $category->id, 'id' => $category->id,