diff --git a/app/Domains/Catalog/Controllers/BrandController.php b/app/Domains/Catalog/Controllers/BrandController.php index 36ffb8c..a4591f3 100644 --- a/app/Domains/Catalog/Controllers/BrandController.php +++ b/app/Domains/Catalog/Controllers/BrandController.php @@ -17,7 +17,7 @@ class BrandController extends Controller public function index(Tenant $tenant): JsonResponse { return BrandResource::collection( - Brand::query()->where('tenant_codigo', $tenant->codigo)->orderByDesc('id')->get() + Brand::query()->where('tenant_codigo', $tenant->codigo)->orderByDesc('id')->paginateFromRequest() )->response(); } diff --git a/app/Domains/Catalog/Controllers/CategoryController.php b/app/Domains/Catalog/Controllers/CategoryController.php index e2357dd..e89a666 100644 --- a/app/Domains/Catalog/Controllers/CategoryController.php +++ b/app/Domains/Catalog/Controllers/CategoryController.php @@ -21,7 +21,7 @@ class CategoryController extends Controller ->where('tenant_code', $tenant->codigo) ->with(['parent', 'subCategories', 'tenant']) ->orderByDesc('id') - ->get() + ->paginateFromRequest() )->response(); } diff --git a/app/Domains/Catalog/Controllers/ProductAttributeController.php b/app/Domains/Catalog/Controllers/ProductAttributeController.php index dff083d..e19c09b 100644 --- a/app/Domains/Catalog/Controllers/ProductAttributeController.php +++ b/app/Domains/Catalog/Controllers/ProductAttributeController.php @@ -23,7 +23,7 @@ class ProductAttributeController extends Controller ->with('options') ->latest(); - return ProductAttributeResource::collection($query->get())->response(); + return ProductAttributeResource::collection($query->paginateFromRequest())->response(); } public function store(StoreProductAttributeRequest $request, Tenant $tenant): JsonResponse diff --git a/app/Domains/Catalog/Controllers/ProductController.php b/app/Domains/Catalog/Controllers/ProductController.php index 59c9918..8b46023 100644 --- a/app/Domains/Catalog/Controllers/ProductController.php +++ b/app/Domains/Catalog/Controllers/ProductController.php @@ -17,7 +17,7 @@ class ProductController extends Controller public function index(Tenant $tenant): JsonResponse { return ProductResource::collection( - Product::query()->where('tenant_codigo', $tenant->codigo)->latest()->get() + Product::query()->where('tenant_codigo', $tenant->codigo)->latest()->paginateFromRequest() )->response(); } diff --git a/app/Domains/Catalog/Controllers/ProductVariantController.php b/app/Domains/Catalog/Controllers/ProductVariantController.php index 3e9967a..4c31702 100644 --- a/app/Domains/Catalog/Controllers/ProductVariantController.php +++ b/app/Domains/Catalog/Controllers/ProductVariantController.php @@ -23,7 +23,7 @@ class ProductVariantController extends Controller ->with(['product', 'definitions.attribute']) ->latest(); - return ProductVariantResource::collection($query->get())->response(); + return ProductVariantResource::collection($query->paginateFromRequest())->response(); } public function store(StoreProductVariantRequest $request, Tenant $tenant): JsonResponse diff --git a/app/Domains/Purchase/Controllers/PurchaseController.php b/app/Domains/Purchase/Controllers/PurchaseController.php index af7085c..1892b4e 100644 --- a/app/Domains/Purchase/Controllers/PurchaseController.php +++ b/app/Domains/Purchase/Controllers/PurchaseController.php @@ -24,7 +24,7 @@ class PurchaseController extends Controller ->where('tenant_codigo', $tenant->codigo) ->where('user_id', $request->user()->id) ->latest() - ->get() + ->paginateFromRequest() )->response(); } diff --git a/app/Domains/Tenant/Controllers/TenantController.php b/app/Domains/Tenant/Controllers/TenantController.php index a6c451d..3857d8e 100644 --- a/app/Domains/Tenant/Controllers/TenantController.php +++ b/app/Domains/Tenant/Controllers/TenantController.php @@ -19,7 +19,7 @@ class TenantController extends Controller public function index(): JsonResponse { - return TenantResource::collection(Tenant::query()->with(['headerLogo', 'footerLogo'])->latest()->get())->response(); + return TenantResource::collection(Tenant::query()->with(['headerLogo', 'footerLogo'])->latest()->paginateFromRequest())->response(); } public function store(StoreTenantRequest $request): JsonResponse diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 452e6b6..5ad1abf 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -19,6 +19,22 @@ class AppServiceProvider extends ServiceProvider */ public function boot(): void { - // + \Illuminate\Database\Eloquent\Builder::macro('paginateFromRequest', function (int $defaultPerPage = 15, int $maxPerPage = 100, ?int $page = null) { + /** @var \Illuminate\Database\Eloquent\Builder $this */ + $perPage = (int) request()->query('per_page', $defaultPerPage); + $page = $page ?? (int) request()->query('page', 1); + + if ($perPage <= 0) { + $perPage = $defaultPerPage; + } + if ($perPage > $maxPerPage) { + $perPage = $maxPerPage; + } + if ($page <= 0) { + $page = 1; + } + + return $this->paginate(perPage: $perPage, page: $page); + }); } }