Skip to main content
wpcrux.com

Back to all posts

How to Create And Use Laravel Facades?

Published on
7 min read
How to Create And Use Laravel Facades? image

Best Laravel Facade Tools to Buy in October 2025

1 Laravel: Up & Running: A Framework for Building Modern PHP Apps

Laravel: Up & Running: A Framework for Building Modern PHP Apps

BUY & SAVE
$41.38 $55.99
Save 26%
Laravel: Up & Running: A Framework for Building Modern PHP Apps
2 Mastering the Snowflake SQL API with Laravel 10: A Comprehensive Guide to Data Cloud Integrated Development (Apress Pocket Guides)

Mastering the Snowflake SQL API with Laravel 10: A Comprehensive Guide to Data Cloud Integrated Development (Apress Pocket Guides)

BUY & SAVE
$15.13 $22.99
Save 34%
Mastering the Snowflake SQL API with Laravel 10: A Comprehensive Guide to Data Cloud Integrated Development (Apress Pocket Guides)
3 Laravel Essentials: Tips & Tricks for Developers: Master Laravel with Practical Tips for Every Developer

Laravel Essentials: Tips & Tricks for Developers: Master Laravel with Practical Tips for Every Developer

BUY & SAVE
$5.99
Laravel Essentials: Tips & Tricks for Developers: Master Laravel with Practical Tips for Every Developer
4 Architecture of complex web applications. Second Edition.: With examples in Laravel(PHP)

Architecture of complex web applications. Second Edition.: With examples in Laravel(PHP)

BUY & SAVE
$0.99
Architecture of complex web applications. Second Edition.: With examples in Laravel(PHP)
5 Laravel 7.X : LEARN BASIC LESSONS & BUILD A CRUD APP (PHP Framework)

Laravel 7.X : LEARN BASIC LESSONS & BUILD A CRUD APP (PHP Framework)

BUY & SAVE
$2.99
Laravel 7.X : LEARN BASIC LESSONS & BUILD A CRUD APP (PHP Framework)
6 Consuming APIs in Laravel: Build Robust and Powerful API Integrations For Your Laravel Projects With Ease

Consuming APIs in Laravel: Build Robust and Powerful API Integrations For Your Laravel Projects With Ease

BUY & SAVE
$39.00
Consuming APIs in Laravel: Build Robust and Powerful API Integrations For Your Laravel Projects With Ease
7 Overview Of Laravel PHP Framework: For Other Web Framework Users

Overview Of Laravel PHP Framework: For Other Web Framework Users

BUY & SAVE
$2.99
Overview Of Laravel PHP Framework: For Other Web Framework Users
8 The Laravel Survival Guide: Written & Updated for Laravel 5.3

The Laravel Survival Guide: Written & Updated for Laravel 5.3

BUY & SAVE
$9.99
The Laravel Survival Guide: Written & Updated for Laravel 5.3
+
ONE MORE?

Laravel facades provide a simple and convenient way to access classes and services in the Laravel framework. By using facades, you can obtain instances of classes without explicitly initializing them. Facades serve as static proxies to underlying classes which means that you can call methods on them as if they are static methods, even though the underlying class may not actually be static.

To create a new facade in Laravel, you need to follow these steps:

  1. Create a new PHP class that will act as the facade. This class should extend the Illuminate\Support\Facades\Facade base class.
  2. In this new facade class, create a protected static method called getFacadeAccessor(). This method should return a string that corresponds to the name or identifier of the service or object you want to access via the facade.
  3. Optionally, you can override the getFacadeAccessor() method to customize the way the facade resolves its underlying object. This is useful when you need to define your own logic to resolve the dependency.
  4. In your facade class, you can define any additional methods that you want to make available as static methods on the facade. These methods will be executed on the underlying object when called.

Once you have created the facade class, you can start using it in your application by referencing the class name statically. For example, if your facade class is named MyFacade, you can use it like this: MyFacade::someMethod(). Laravel will resolve the facade's underlying object using the getFacadeAccessor() method and then execute the desired method on that object.

Using Laravel facades can simplify your code and make it more readable. It allows you to access important services and components without having to initialize or inject them explicitly throughout your codebase. Laravel provides several built-in facades, such as DB to access the database, Auth for authentication, and Cache for caching, among others.

Remember to import the facade class at the top of your files or you can use the fully qualified names when calling methods on facades. For example: Illuminate\Support\Facades\DB::table('users')->get().

How to define the contract for a Laravel facade?

To define the contract for a Laravel facade, you need to create an interface that specifies the methods and functionality that the facade will provide. Follow these steps to define the contract for a Laravel facade:

  1. Create a new interface file: Start by creating a new file in the 'app/Contracts' directory or any other appropriate location. Name the file with the name of the facade you want to create, followed by 'Interface.php' (e.g., MyFacadeInterface.php).
  2. Define the interface: Inside the interface file, define the methods that the facade should implement. You can define any number of methods in the interface, depending on the functionality you want to expose through the facade. For example: