LaravelMarch 15, 2026

Understanding Laravel Facades: How They Work

Learn how Laravel facades work under the hood and when to use them.

Understanding Laravel Facades: How They Work

Facades provide a static interface to classes available in the application’s service container. Let’s understand how they work.

How Facades Work

php
use Illuminate\Support\Facades\Cache;

Cache::get('key');

This is equivalent to:

php
app('cache')->get('key');

Creating Custom Facades

php
namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class Payment extends Facade
{
    protected static function getFacadeAccessor(): string
    {
        return 'payment';
    }
}

Register in your service provider:

php
public function register(): void
{
    $this->app->singleton('payment', function ($app) {
        return new PaymentGateway($app['config']['payment']);
    });
}

Real-Time Facades

php
use Facades\App\Services\PaymentService;

PaymentService::process($order);

When to Use Facades

  • Quick prototyping
  • Simple applications
  • When testability isn’t a primary concern

When to Avoid Facades

  • When building large applications
  • When you need clear dependency declarations
  • In domain logic where explicit dependencies are preferred