diff --git a/app/Http/Controllers/Api/V1/Filters/FilterController.php b/app/Http/Controllers/Api/V1/Filters/FilterController.php index d326d85..d40a442 100644 --- a/app/Http/Controllers/Api/V1/Filters/FilterController.php +++ b/app/Http/Controllers/Api/V1/Filters/FilterController.php @@ -62,29 +62,29 @@ class FilterController extends Controller private function brands() { if ($this->shouldFilterByBrand()) { - return Brand::where('id', $this->request->brand_id)->get(['id', 'name']); + return Brand::query()->where('id', $this->request->brand_id)->get(['id', 'name']); } if ($this->shouldFilterByCategory()) { - $brands = Category::find($this->request->category_id)->products() + $brands = Category::query()->find($this->request->category_id)->products() ->where('products.is_visible', true) ->where('products.parent_id', null) ->where('products.stock', '>', 0) ->distinct('products.brand_id') ->pluck('products.brand_id'); - return Brand::whereIntegerInRaw('id', $brands)->get(['id', 'name']); + return Brand::query()->whereIntegerInRaw('id', $brands)->get(['id', 'name']); } if ($this->shouldFilterByCollection()) { - $brands = Collection::find($this->request->collection_id)->products() + $brands = Collection::query()->find($this->request->collection_id)->products() ->where('products.is_visible', true) ->where('products.parent_id', null) ->where('products.stock', '>', 0) ->distinct('products.brand_id') ->pluck('products.brand_id'); - return Brand::whereIntegerInRaw('id', $brands)->get(['id', 'name']); + return Brand::query()->whereIntegerInRaw('id', $brands)->get(['id', 'name']); } return Brand::query()->where('is_visible', true)->ordered()->get(['id', 'name']); @@ -102,13 +102,18 @@ class FilterController extends Controller ->distinct('products.id') ->pluck('products.id'); - return Category::where('is_visible', true) - ->ordered() - ->join('product_has_relations', 'categories.id', '=', 'product_has_relations.productable_id') - ->where('product_has_relations.productable_type', '=', 'category') - ->whereIntegerInRaw('product_has_relations.product_id', $products) - ->get(['categories.id', 'categories.parent_id', 'categories.name']) - ->unique('categories.id'); + return Category::query() + ->distinct('categories.id') + ->where('is_visible', true) + ->join('product_has_relations', 'categories.id', '=', 'product_has_relations.productable_id') + ->where('product_has_relations.productable_type', '=', 'category') + ->whereIntegerInRaw('product_has_relations.product_id', $products) + ->get() + ->map(fn ($category) => [ + 'id' => $category->id, + 'parent_id' => $category->parent_id, + 'name' => $category->name, + ]); } /** diff --git a/app/Models/User.php b/app/Models/User.php index 31bc7a7..6d894d7 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -17,11 +17,11 @@ use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; use Spatie\DeletedModels\Models\Concerns\KeepsDeletedModels; use Spatie\Permission\Traits\HasRoles; -use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable {