Overview
Catalog Services are responsible for providing data to populate UI elements like dropdowns, filters, and form selects. They centralize data queries and apply global filters (likecountry_id) automatically.
Naming Convention
Catalog Services follow a strict naming pattern:app/Services/Sales/SalesServices/SaleCatalogService.phpapp/Services/Client/ClientCatalogService.phpapp/Services/Inventory/WarehouseService/WarehouseCatalogService.php
Standard Methods
Every Catalog Service should implement two primary methods:getForFilters()
Provides data for filter dropdowns in index/list views. Returns minimal data optimized for filtering.getForForm()
Provides comprehensive data for create/edit forms. Includes related data and computed fields.Real-World Example: Sales Module
Let’s examine the completeSaleCatalogService from the system:
app/Services/Sales/SalesServices/SaleCatalogService.php
Key Patterns
1. Country-Aware Filtering
Many catalogs need to filter by the system’s configured country:app/Services/Client/ClientCatalogService.php
2. Computed Fields
Add computed fields that the frontend needs but aren’t stored in the database:3. Conditional Data Loading
Only load data when relationships exist:4. Custom Sorting
Prioritize specific records using SQL:5. Scoped Queries
Use model scopes for reusable filters:Performance Optimization
Select Specific Columns
Eager Loading
Use eager loading to avoid N+1 queries:Selective Eager Loading
Limit fields in eager loaded relationships:Static vs Dynamic Data
Static Data (Model Constants)
For fixed options, use model constants:app/Models/Sales/Sale.php
Dynamic Data (Database)
For configurable options, query the database:Controller Integration
Here’s how to use Catalog Services in controllers:app/Http/Controllers/Sales/SaleController.php
Template Example
Use this template for new Catalog Services:app/Services/[Module]/[Module]CatalogService.php
Testing Catalog Services
Catalog Services are easy to unit test:tests/Unit/Services/SaleCatalogServiceTest.php
Best Practices
Read-Only
Catalog Services should never modify data. They only provide it for UI consumption.
Optimize Queries
Always select specific columns and use eager loading to prevent N+1 queries.
Apply Global Filters
Respect system-wide settings like
country_id when filtering data.Consistent Structure
Return arrays with consistent keys. Always include
id and display fields.Common Pitfalls
Next Steps
Business Services
Learn how to implement write operations and business logic
Form Requests
Validate data before it reaches services
Adding Modules
Complete guide to implementing new modules
Models
Understand model relationships and scopes