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 

To Top