Explain Laravel Design Patterns used in projects.
Several design patterns are frequently used in Laravel projects to maintain scalable, reusable, and clean code. Numerous patterns are already implemented internally by Laravel, which is based on PHP and was inspired by MVC architecture.
π· 1. MVC (Model-View-Controller)
This is the core pattern in Laravel.
- Model β Handles database (Eloquent ORM)
- View β UI (Blade templates)
- Controller β Business logic
Example:
-
Model:
User.php -
Controller:
UserController.php -
View:
user.blade.php
π Keeps logic separated and organized.
π· 2. Singleton Pattern
Ensures a class has only one instance throughout the application.
Used in:
-
Service Container (
App::singleton())
Example:
$this->app->singleton(MyService::class, function ($app) {
return new MyService();
});
π Useful for shared services like logging, config, etc.
π· 3. Factory Pattern
Used to create objects without specifying exact class logic.
Used in:
- Model Factories (for testing & seeding)
Example:
User::factory()->create();
π Helps generate fake/test data easily.
π· 4. Repository Pattern
Separates data access logic from controllers.
Structure:
-
Interface:
UserRepositoryInterface -
Class:
UserRepository
Example:
interface UserRepositoryInterface {
public function getAllUsers();
}
π Makes code cleaner and easier to maintain or switch DB logic.
π· 5. Service Pattern
Moves business logic into service classes.
Example:
class PaymentService {
public function processPayment($data) {
// business logic
}
}
π Keeps controllers thin and focused.
π· 6. Dependency Injection (DI)
Laravel automatically injects dependencies into constructors.
Example:
public function __construct(UserRepositoryInterface $userRepo) {
$this->userRepo = $userRepo;
}
π Improves testability and flexibility.
π· 7. Facade Pattern
Provides a static interface to classes in the service container.
Examples:
-
Cache -
Route -
DB
Cache::put('key', 'value', 10);
π Easy syntax but still uses underlying services.
π· 8. Observer Pattern
Used to listen to model events.
Example:
User::created(function ($user) {
// send email
});
π Useful for event-driven actions.
π· 9. Strategy Pattern
Defines multiple algorithms and lets you switch between them.
Example:
Different payment methods:
- CreditCardPayment
- UpiPayment
- WalletPayment
π Select logic dynamically at runtime.
π· 10. Builder Pattern
Used to build complex queries step by step.
Example:
User::where('status', 1)
->orderBy('name')
->get();
π Clean and readable query building.
π· 11. Adapter Pattern
Used when integrating third-party libraries.
π Example:
- Payment gateway integration
- External APIs
π· 12. Command Pattern
Encapsulates actions as objects.
Used in:
-
Laravel Jobs (
php artisan make:job) - Queues
π Helps in background processing.
β Real Project Usage Flow
In a typical Laravel project:
- Controller β calls Service
- Service β uses Repository
- Repository β interacts with Model
- Events/Observers β trigger side actions
π₯ Best Practices
- Use Repository + Service pattern in large projects
- Keep controllers thin
- Use DI instead of new keyword
- Avoid putting business logic in models/controllers