Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

Efficient Context Management with Laravel's Remember Functions

Published on by

Efficient Context Management with Laravel's Remember Functions image

Laravel's Context API introduces powerful remember() and rememberHidden() methods that streamline request-scoped data management through elegant closure-based memoization. These functions optimize expensive computations across your application with clean, expressive syntax.

The remember() function provides automatic memoization within the request context:

use Illuminate\Support\Facades\Context;
 
$roles = Context::remember('user-roles', fn() => $this->calculateUserRoles(auth()->user()));

This method automatically manages the existence check and computation cycle, executing the closure only when the context key is absent.

Consider a multi-service architecture where request-scoped data like organization settings, access controls, and feature configurations must be shared efficiently across various application layers. These values are computationally expensive but frequently accessed throughout the request lifecycle:

<?php
 
namespace App\Services;
 
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Facades\DB;
use App\Models\Organization;
use App\Models\User;
 
class OrganizationContextService
{
public function getOrganizationSettings(): array
{
return Context::remember('org-settings', function () {
$organization = $this->getCurrentOrganization();
 
return [
'subscription_tier' => $this->getSubscriptionTier($organization),
'enabled_modules' => $this->getEnabledModules($organization),
'user_limits' => $this->getUserLimits($organization),
'integration_settings' => $this->getIntegrationSettings($organization),
];
});
}
 
public function getCurrentUserAccessLevel(): array
{
return Context::remember('user-access-level', function () {
$user = auth()->user();
$organization = $this->getCurrentOrganization();
 
$directPermissions = $user->permissions()
->where('organization_id', $organization->id)
->pluck('action')
->toArray();
 
$groupPermissions = $user->groups()
->with('permissions')
->get()
->flatMap(fn($group) => $group->permissions)
->pluck('action')
->toArray();
 
$inheritedPermissions = $this->calculateInheritedPermissions($user, $organization);
 
return array_unique(array_merge(
$directPermissions,
$groupPermissions,
$inheritedPermissions
));
});
}
 
public function getSecureCredentials(): array
{
return Context::rememberHidden('secure-credentials', function () {
$organization = $this->getCurrentOrganization();
 
return [
'api_secret' => decrypt($organization->encrypted_api_secret),
'webhook_token' => $this->generateWebhookToken($organization),
'internal_key' => $this->getInternalEncryptionKey($organization),
];
});
}
 
public function hasFeatureAccess(string $feature): bool
{
$settings = $this->getOrganizationSettings();
$userAccess = $this->getCurrentUserAccessLevel();
 
return in_array($feature, $settings['enabled_modules']) &&
in_array("feature:{$feature}", $userAccess);
}
 
protected function getCurrentOrganization(): Organization
{
return Context::remember('current-organization', function () {
$subdomain = request()->route('organization') ?? request()->getHost();
 
return Organization::where('subdomain', $subdomain)
->with(['subscription', 'settings'])
->firstOrFail();
});
}
 
protected function getSubscriptionTier(Organization $organization): string
{
return $organization->subscription->tier ?? 'basic';
}
 
protected function getEnabledModules(Organization $organization): array
{
$baseTier = $this->getSubscriptionTier($organization);
$customModules = $organization->settings->pluck('module')->toArray();
 
return array_merge(
$this->getDefaultModulesForTier($baseTier),
$customModules
);
}
 
protected function getUserLimits(Organization $organization): array
{
$tier = $this->getSubscriptionTier($organization);
 
return match($tier) {
'enterprise' => ['max_users' => -1, 'storage_gb' => 1000],
'professional' => ['max_users' => 100, 'storage_gb' => 100],
'basic' => ['max_users' => 10, 'storage_gb' => 10],
default => ['max_users' => 5, 'storage_gb' => 1],
};
}
 
protected function getIntegrationSettings(Organization $organization): array
{
return $organization->integrations()
->where('active', true)
->get()
->mapWithKeys(fn($integration) => [
$integration->service => $integration->configuration
])
->toArray();
}
 
protected function calculateInheritedPermissions(User $user, Organization $organization): array
{
if ($user->is_organization_owner) {
return ['admin:*', 'user:*', 'billing:*', 'settings:*'];
}
 
if ($user->is_manager) {
return ['user:manage', 'reports:view', 'settings:view'];
}
 
return ['profile:edit', 'dashboard:view'];
}
 
protected function generateWebhookToken(Organization $organization): string
{
return hash_hmac('sha256', $organization->id . now()->timestamp, config('app.key'));
}
 
protected function getInternalEncryptionKey(Organization $organization): string
{
return hash('sha256', config('app.key') . $organization->uuid);
}
 
protected function getDefaultModulesForTier(string $tier): array
{
return match($tier) {
'enterprise' => ['analytics', 'integrations', 'advanced_reports', 'api_access'],
'professional' => ['analytics', 'basic_reports', 'limited_api'],
'basic' => ['dashboard', 'basic_reports'],
default => ['dashboard'],
};
}
}
 
class ReportingController extends Controller
{
public function __construct(
private OrganizationContextService $contextService
) {}
 
public function generateReport(Request $request)
{
if (!$this->contextService->hasFeatureAccess('advanced_reports')) {
abort(403, 'Advanced reporting not available for current plan');
}
 
$settings = $this->contextService->getOrganizationSettings();
$userAccess = $this->contextService->getCurrentUserAccessLevel();
 
return view('reports.advanced', compact('settings', 'userAccess'));
}
 
public function exportData(Request $request)
{
$credentials = $this->contextService->getSecureCredentials();
 
if (!$this->verifyExportPermissions($credentials['internal_key'])) {
abort(401, 'Invalid export permissions');
}
 
return $this->processDataExport($request);
}
}

The remember() function ensures expensive operations like permission calculations and organization lookups execute only once per request, regardless of access frequency. The rememberHidden() variant maintains identical caching behavior while excluding sensitive data from log entries, making it ideal for credentials and security tokens.

This pattern excels in middleware layers, service dependencies, and view composers where identical context data requires multiple access points. The closure-based design creates self-documenting code that clearly indicates the computation triggered during cache misses.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
SerpApi logo

SerpApi

Access real-time search engine results through a simple API—no more scraping headaches! Use it for AI applications, SEO tools, product research, travel information, and more

SerpApi

The latest

View all →
Drag-and-Drop Sorting for Eloquent Models with Reorderable for Laravel image

Drag-and-Drop Sorting for Eloquent Models with Reorderable for Laravel

Read article
Ship AI with Laravel: Real-Time Streaming Chat UI with Livewire image

Ship AI with Laravel: Real-Time Streaming Chat UI with Livewire

Read article
Frontend Nation 2026 Returns June 3-4 with Laravel in the Lineup image

Frontend Nation 2026 Returns June 3-4 with Laravel in the Lineup

Read article
Use a Google Sheet as Your Laravel Database with the Google Sheets Database Driver image

Use a Google Sheet as Your Laravel Database with the Google Sheets Database Driver

Read article
Larapanda: A Type-Safe Lightpanda Browser SDK for Laravel image

Larapanda: A Type-Safe Lightpanda Browser SDK for Laravel

Read article
Generate HTML Password Rules Attribute in Laravel 13.9.0 image

Generate HTML Password Rules Attribute in Laravel 13.9.0

Read article