Primarily responsible for handling HTTP requests and delegating business logic to the Service Layer. It should be as lean as possible.
Contains the core business logic, orchestrating operations between repositories, models, and other services. Promotes reusability and testability of complex operations.
Abstracts the data persistence layer. It provides methods for interacting with the database, shielding the Service Layer from direct ORM (Eloquent) calls for basic CRUD. This allows for easier switching of data sources and improves testability.
Encapsulate complex query building logic. This pattern centralizes, reuses, and improves the readability of database queries.
Centralizes and standardizes the extraction and normalization of filtering, sorting, and pagination parameters from HTTP requests.
Transform Eloquent models into a consistent JSON structure for API responses, controlling data exposure and formatting.
Separates and centralizes input validation logic, keeping controllers clean and providing consistent error responses.
Laravel's powerful Service Container is used to inject dependencies into classes, promoting loose coupling and testability.
Used for classes that should only have one instance throughout the application's lifecycle (e.g., stateless utility classes like Query Objects or Filters). Laravel's container manages this efficiently.
Ensures atomicity of multiple database operations, maintaining data integrity.
Laravel is built on MVC.
authorize() method is for checking user
permissions before validation.public function __construct(ProductService $productService) in
ProductController.Eloquent is incredibly powerful and, when used correctly, can lead to highly optimized queries.
with() to eager load relationships.// Example: If Product had a 'category' relationship
$products = Product::with('category')->paginate(10);
$query->paginate($perPage); in
ProductService.Product::active()->get();Product::select('id', 'name', 'price')->get();$query->when($search, function ($query, $search) {
return $query->where('name', 'like', '%' . $search . '%')
->orWhere('description', 'like', '%' . $search . '%');
});
$product->load('reviews');Product::chunk(200, function ($products) {
foreach ($products as $product) {
// Process product
}
});
Product::count();GET /products: Retrieve a list of products (index).GET /products/{id}: Retrieve a specific product (show).POST /products: Create a new product (store).PUT /products/{id}: Update an existing product (update).DELETE /products/{id}: Delete a product (destroy).Example (Success):
{
"success": true,
"message": "Product created successfully.",
"data": {
// Product object
}
}
Example (Error):
{
"success": false,
"message": "Failed to create product.",
"error": "Validation failed: The name field is required."
}
200 OK: Successful GET, PUT, PATCH, DELETE.201 Created: Successful POST.204 No Content: Successful DELETE.400 Bad Request: Invalid request payload.401 Unauthorized: Authentication required.403 Forbidden: Authenticated but not authorized.404 Not Found: Resource not found.422 Unprocessable Entity: Validation errors.500 Internal Server Error: Server-side error.php artisan make:resource ProductResourceapp/Providers/RouteServiceProvider.php.Essential packages for Laravel API development:
composer require spatie/laravel-activitylog
composer require unisharp/laravel-filemanager
composer require spatie/laravel-permission
composer require maatwebsite/excel
composer require opcodesio/log-viewer
composer require laravel/telescope
composer require barryvdh/laravel-debugbar
composer require yajra/laravel-datatables-oracle
composer require barryvdh/laravel-dompdf
composer require doctrine/dbal