Overview
We’ll create:- Model - Product model with relations
- Service - Product service layer
- Controller - RESTful API controller
- Routes - API endpoint definitions
- HTTP Examples - Complete request/response examples
Step 1: Create the Model
Createapp/Models/Product.php:
Step 2: Create the Service
Createapp/Services/ProductService.php:
Step 3: Create the Controller
Createapp/Http/Controllers/Api/ProductController.php:
Step 4: Define Routes
Add toroutes/api.php:
Step 5: Database Migration
Create the products table:HTTP Request Examples
Create a Product
List Products with Filtering
Get Single Product
Update a Product
Delete a Product
Bulk Update Products
Advanced Filtering Examples
Search with Multiple Conditions
Filter by Related Model
Testing Your API
1
Seed Test Data
Create a seeder to populate test products:
2
Test with cURL
3
Test with Postman
Import the API endpoints into Postman and test all CRUD operations with various filter combinations.
All endpoints automatically handle validation, error responses, and database exceptions. The BaseService provides built-in caching, filtering, and relation loading out of the box.
Next Steps
- Explore Filtering Scenarios for advanced query examples
- Learn about Cache Scenarios for performance optimization
- Check Edge Cases for troubleshooting common issues