edulazaro/laracontext

Context for Laravel

1.0 2025-06-08 01:45 UTC

This package is auto-updated.

Last update: 2025-06-08 01:47:47 UTC


README

Total Downloads Latest Stable Version

Introduction

Laracontext is a simple lightweight package that enables to set a global context, and also enables scoped queries based on shared contextual values across your Laravel application.

Global Context StorageScoped Eloquent QueriesSupport for Dot Notation & Model InjectionClear, Restore, and Snapshot Context State

Installation

Install via Composer:

composer require edulazaro/laracontext

Once installed, the package will be available in your Laravel application.

Basic Usage

You can set and get context values using the helper function:

// Set single value
context(['tenant_id' => 5]);

// Get a value
$tenantId = context('tenant_id'); // 5

// Set multiple values
context([
    'group.id' => 12,
    'group.name' => 'Admins'
]);

// Get a nested value
$groupId = context('group.id'); // 12

You can also use the Context class directly:

use EduLazaro\Laracontext\Context;

// Resolve the context instance
$context = app(Context::class);

// Set values
$context->set('user.id', 1);
$context->set(['locale' => 'en']);

// Get values
$id = $context->get('user.id');
$locale = $context->get('locale');

// Check existence
$context->has('user.id'); // true

// Forget a value
$context->forget('user.id');

// Clear all context
$context->clear();

You can also treat it like an array:

$context['timezone'] = 'Europe/Madrid';
echo $context['timezone']; // Europe/Madrid

Scoped Queries with HasContextScope

You can filter Eloquent queries automatically based on context using the HasContextScope trait.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use EduLazaro\Laracontext\Concerns\HasContextScope;

class Dog extends Model
{
    use HasContextScope;

    public static array $context = ['tenant_id'];
}

Now, if you set:

context(['tenant_id' => 1]);

You can query filtered results with:

Dog::context()->get();

It will generate the equivalent of:

Dog::where('tenant_id', 1)->get();

You can also map context keys differently:

public static array $context = [
    'tenant_id' => 'custom.tenant'
];

This will match:

context(['custom.tenant' => 1]);

Dog::context(); // where tenant_id = 1

It even supports passing a model as the context value:

context(['tenant' => $tenantModel]);

The trait will call $tenantModel->getKey() automatically.

Snapshot and Restore

You can snapshot the context to save and later restore it:

$snapshot = context()->snapshot();

context()->clear();

// ...do stuff...

context()->restore($snapshot);

Testing

In tests, always clear context before each test to avoid leaks:

context()->clear();

You can also bind a fresh instance for total isolation:

$this->app->singleton(Context::class, fn () => new Context());

In order to run the test, rung:

./vendor/bin/phpunit 

License

Laractions is open-sourced software licensed under the MIT license.