This commit is contained in:
Mekan1206
2026-05-03 18:42:16 +05:00
parent 1b467108de
commit bac2ad9a3e

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()) {
@@ -111,6 +112,35 @@ class FilterController extends Controller
->unique('categories.id'); ->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()
->map(fn ($category) => [
'id' => $category->id,
'parent_id' => $category->parent_id,
'name' => $category->name,
]);
}
/** /**
* Check if request should be filtered by category * Check if request should be filtered by category
*/ */