PHPackages                             edulazaro/laracontext - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. edulazaro/laracontext

ActiveLibrary[Framework](/categories/framework)

edulazaro/laracontext
=====================

Context for Laravel

1.0(11mo ago)0423↓50%MITPHPPHP ^8.2

Since Jun 8Pushed 11mo agoCompare

[ Source](https://github.com/edulazaro/laracontext)[ Packagist](https://packagist.org/packages/edulazaro/laracontext)[ RSS](/packages/edulazaro-laracontext/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

Laracontext - Context for Laravel
=================================

[](#laracontext---context-for-laravel)

 [![Total Downloads](https://camo.githubusercontent.com/d40b6ff4c50b3f89439ba88dafffc97d48ac9856289ee7bd27a197eaf7aa7353/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6564756c617a61726f2f6c617261636f6e74657874)](https://packagist.org/packages/edulazaro/laracontext) [![Latest Stable Version](https://camo.githubusercontent.com/1dc9f845e61fee4f852d67d7a9238127809747b0a4423455091c9cefd8faa727/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6564756c617a61726f2f6c617261636f6e74657874)](https://packagist.org/packages/edulazaro/laracontext)

Introduction
------------

[](#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 Storage**✅ **Scoped Eloquent Queries**✅ **Support for Dot Notation &amp; Model Injection**✅ **Clear, Restore, and Snapshot Context State**

Installation
------------

[](#installation)

Install via Composer:

```
composer require edulazaro/laracontext
```

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

Basic Usage
-----------

[](#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
-----------------------------------

[](#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
--------------------

[](#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
-------

[](#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
-------

[](#license)

Laractions is open-sourced software licensed under the [MIT license](LICENSE.md).

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance52

Moderate activity, may be stable

Popularity16

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

337d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6a3c47449dfb2ec121aa410da024f47586b87cc2799a825f0418e6c5e5904955?d=identicon)[edulazaro](/maintainers/edulazaro)

---

Top Contributors

[![edulazaro](https://avatars.githubusercontent.com/u/7797530?v=4)](https://github.com/edulazaro "edulazaro (1 commits)")

### Embed Badge

![Health badge](/badges/edulazaro-laracontext/health.svg)

```
[![Health](https://phpackages.com/badges/edulazaro-laracontext/health.svg)](https://phpackages.com/packages/edulazaro-laracontext)
```

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M192](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M257](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M593](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
