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(1y ago)0523↓78.6%MITPHPPHP ^8.2

Since Jun 8Pushed 1y agoCompare

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

READMEChangelog (1)Dependencies (3)Versions (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

32

—

LowBetter than 69% of packages

Maintenance47

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

391d 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)")

###  Code Quality

TestsPHPUnit

### 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/octane

Supercharge your Laravel application's performance.

4.0k26.6M223](/packages/laravel-octane)[unopim/unopim

UnoPim Laravel PIM

10.5k2.4k](/packages/unopim-unopim)[code16/sharp

Laravel Content Management Framework

79164.7k8](/packages/code16-sharp)[ecotone/laravel

Ecotone for Laravel — CQRS, Event Sourcing, Sagas, Durable Workflows, and Outbox on top of Laravel Queue, via PHP attributes.

21318.6k3](/packages/ecotone-laravel)[codewithdennis/larament

Larament is a time-saving starter kit to quickly launch Laravel 13.x projects. It includes FilamentPHP 5.x pre-installed and configured, along with additional tools and features to streamline your development workflow.

3891.8k](/packages/codewithdennis-larament)[r2luna/brain

Brain: A process-driven architecture alternative for your Laravel Application.

6338.7k1](/packages/r2luna-brain)

PHPackages © 2026

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