1. Go to this page and download the library: Download edulazaro/laracontext library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
edulazaro / laracontext example snippets
// 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
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();
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'];
}
context(['tenant_id' => 1]);
Dog::context()->get();
Dog::where('tenant_id', 1)->get();
public static array $context = [
'tenant_id' => 'custom.tenant'
];
context(['custom.tenant' => 1]);
Dog::context(); // where tenant_id = 1