In this tutorial, we’ll build a Products & Categories API in Laravel 12 using JWT authentication. We’ll follow Laravel’s RESTful principles with clean controllers, routes, and proper model relationships.
📌 API Endpoints Overview
Categories
Method | Endpoint | Purpose |
---|---|---|
GET | /api/categories | List all categories |
POST | /api/categories | Create a category |
GET | /api/categories/{id} | Show category details |
PUT | /api/categories/{id} | Update category |
DELETE | /api/categories/{id} | Delete category |
Products
Method | Endpoint | Purpose |
---|---|---|
GET | /api/products | List all products |
POST | /api/products | Create a product |
GET | /api/products/{id} | Show product details |
PUT | /api/products/{id} | Update product |
DELETE | /api/products/{id} | Delete product |
Step 1: Create Models & Migrations
Migration: database/migrations/xxxx_xx_xx_create_categories_table.php
Migration: database/migrations/xxxx_xx_xx_create_products_table.php
Run migrations:
Step 2: Define Relationships
app/Models/Category.php
app/Models/Product.php
Step 3: Create Controllers
Step 4: CategoryController
app/Http/Controllers/Api/CategoryController.php
Step 5: ProductController
app/Http/Controllers/Api/ProductController.php
Step 6: Define Routes
routes/api.php
Step 7: Testing with Postman
Categories
-
GET /api/categories
→ List categories -
POST /api/categories
→ Create category
-
GET /api/categories/1
→ Show category -
PUT /api/categories/1
→ Update category
-
DELETE /api/categories/1
→ Delete category
Products
-
GET /api/products
→ List products with category details -
POST /api/products
→ Create product
-
GET /api/products/1
→ Show product -
PUT /api/products/1
→ Update product
-
DELETE /api/products/1
→ Delete product
🎯 Conclusion
You now have a fully functional Products & Categories API in Laravel 12 secured with JWT authentication.
This setup includes:
-
✅ Category management (CRUD)
-
✅ Product management (CRUD with category relation)
-
✅ Clean RESTful controllers & routes
-
✅ Postman-tested endpoints
With this foundation, you can further extend the API by adding authentication, user roles, and permissions as needed. 🚀
Want the full source code?
Download the complete Laravel 12 JWT API Authentication example on my GitHub repo here.
Happy Coding!