Laravel PHP Software Development

Optimizing Laravel Components: The Request-Scoped Singleton Pattern

The Problem: Redundant Database Queries

Imagine you have a Laravel component that fetches data from the database. This component is used multiple times in different parts of your view, like this:

<!-- In your Blade view -->
@include('', ['type' => 'users'])
@include('', ['type' => 'posts'])
@include('', ['type' => 'comments'])

If each inclusion of this component runs its own database query, you're potentially running the same query multiple times per page load. This is inefficient and can slow down your application.

The Solution: Request-Scoped Singleton Pattern

We can optimize this by ensuring the database query runs only once per request, regardless of how many times the component is used. Here's how:

  1. We'll create a single instance of our component class for each request.
  2. This instance will store the query results.
  3. All uses of the component within the same request will share these results.


Here's how we can implement this pattern:

class DataFetcher
    private static $instance = null;
    private $data = null;

    public function __construct()
        if (self::$instance === null) {
            self::$instance = $this;
        } else {
            $this->data = self::$instance->data;

    public function getData($type)
        if ($this->data === null) {
            $this->data = $this->fetchAllData();

        return $this->data[$type] ?? [];

    private function fetchAllData()
        // This query runs only once per request
        return [
            'users' => DB::table('users')->get(),
            'posts' => DB::table('posts')->get(),
            'comments' => DB::table('comments')->get(),

And in your component view:

$dataFetcher = new DataFetcher();
$data = $dataFetcher->getData($type);

<!-- Display $data here -->

How It Works

  1. Single Instance: The static $instance property ensures only one instance of DataFetcher exists per request.
  2. Data Sharing: All DataFetcher objects created in the same request share the same $data.
  3. Lazy Loading: The database query only runs when getData() is first called, not on object creation.


  1. Performance: The database is queried only once per request, no matter how many times the component is used.
  2. Consistency: All instances of the component use the same data within a request.
  3. Simplicity: The optimization is hidden inside the DataFetcher class. The way you use the component in your views doesn't change.

When to Use This Pattern

This pattern is useful when:

  • You have data that's used in multiple places in a single page load.
  • The data doesn't change during the request.
  • Fetching the data is an expensive operation (like a complex database query).


  • This pattern works per request. The data isn't shared between different requests or users.
  • For data that rarely changes, consider using Laravel's caching system instead.

Leave a Reply

Your email address will not be published. Required fields are marked *