Download the PHP package edulazaro/laracontext without Composer
On this page you can find all versions of the php package edulazaro/laracontext. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laracontext
Laracontext - Context for Laravel
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 & Model Injection ✅ Clear, Restore, and Snapshot Context State
Installation
Install via Composer:
Once installed, the package will be available in your Laravel application.
Basic Usage
You can set and get context values using the helper function:
You can also use the Context
class directly:
You can also treat it like an array:
Scoped Queries with HasContextScope
You can filter Eloquent queries automatically based on context using the HasContextScope
trait.
Now, if you set:
You can query filtered results with:
It will generate the equivalent of:
You can also map context keys differently:
This will match:
It even supports passing a model as the context value:
The trait will call $tenantModel->getKey()
automatically.
Snapshot and Restore
You can snapshot the context to save and later restore it:
Testing
In tests, always clear context before each test to avoid leaks:
You can also bind a fresh instance for total isolation:
In order to run the test, rung:
License
Laractions is open-sourced software licensed under the MIT license.