tenant-context-guide

Resolve and persist tenant context across Laravel requests and queued jobs.

2|Updated Nov 16, 2024
One-click install
npx skills add https://github.com/onairmarc/dotfiles --skill tenant-context-guide
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tenant-context-guide
Source: https://github.com/onairmarc/dotfiles/tree/main/.agents/skills/tenant-context-guide
Command: npx skills add https://github.com/onairmarc/dotfiles --skill tenant-context-guide

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Developers often lose track of the tenant context in multi-tenant Laravel apps, leading to inconsistent data access or leaks.

Core Features & Use Cases

  • Automatic context propagation: Tenant context is resolved once and persisted across the request or job lifecycle.
  • Context switching in loops: Use TenantContext::applyById($tenantId, true) when iterating over multi-tenant records to switch context safely.
  • Safe default behavior: Avoid forgetting Context::forget by adhering to the recommended usage.
  • Use cases include middleware-initiated requests, queued jobs, and batch processing across tenants.

Quick Start

Switch to a tenant's context inside a loop using TenantContext::applyById($tenantId, true) before performing tenant-scoped work.

Frequently Asked Questions about tenant-context-guide

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I maintain tenant context across queued jobs in a multi-tenant Laravel app?

Maintaining tenant context across queued jobs in a multi-tenant Laravel app requires resolving the context once and automatically persisting it throughout the request or job lifecycle without needing re-application.

What is the best way to switch tenant context safely when iterating over multi-tenant records in a loop?

The best way to switch tenant context safely in loops is calling TenantContext::applyById($tenantId, true) before performing tenant-scoped work, which prevents forgetting to clear the context.

Why does my multi-tenant Laravel application leak data across different tenants during batch processing?

Multi-tenant Laravel applications leak data during batch processing when tenant context is lost or forgotten, which this Skill prevents by automatically propagating the resolved context across the entire request lifecycle.

Can I use automatic tenant context propagation for middleware-initiated web requests in PHP?

Yes, automatic tenant context propagation works for middleware-initiated web requests in PHP by resolving the tenant context once and persisting it across the entire request lifecycle without manual re-application.

Do I need to manually call Context::forget when switching tenants during multi-tenant batch processing?

No, you do not need to manually call Context::forget if you adhere to the recommended usage of TenantContext::applyById($tenantId, true), which ensures safe default behavior when switching tenants.