1. Go to this page and download the library: Download litepie/organization 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/ */
// In your route files
Route::middleware(['tenant.ionController::class);
});
// Detect by domain
// tenant1.myapp.com -> tenant1
// Detect by header
// X-Tenant-ID: 123
// Detect by authenticated user
// auth()->user()->tenant_id
// Custom detection in your AppServiceProvider
app()->bind(TenantDetectorContract::class, CustomTenantDetector::class);
use Litepie\Organization\Models\Organization;
// Create a company
$company = Organization::create([
'type' => 'company',
'name' => 'Acme Corporation',
'code' => 'ACME',
'status' => 'active',
'created_by' => auth()->id(),
]);
// Create a branch under the company
$branch = Organization::create([
'parent_id' => $company->id,
'type' => 'branch',
'name' => 'New York Branch',
'code' => 'ACME-NY',
'status' => 'active',
'created_by' => auth()->id(),
]);
use Litepie\Tenancy\Facades\Tenancy;
// Organizations are automatically filtered by current tenant
$organizations = Organization::all(); // Only current tenant's organizations
// Get current tenant information
$tenant = Tenancy::current();
$tenantId = $tenant?->getTenantId();
// Bypass tenant scoping (admin operations)
$allOrganizations = Organization::withoutTenantScope()->get();
// Manually execute in specific tenant context
$tenant->execute(function () {
$organizations = Organization::all(); // Scoped to this tenant
});
// Switch tenant context for operations
Tenancy::setTenant($anotherTenant);
$organizations = Organization::all(); // Now scoped to different tenant
// Get organizations across multiple tenants (admin view)
$crossTenantStats = Organization::withoutTenantScope()
->selectRaw('tenant_id, count(*) as total')
->groupBy('tenant_id')
->get();
// Execute operations for each tenant
foreach (Tenancy::getAllTenants() as $tenant) {
$tenant->execute(function () use ($tenant) {
$count = Organization::count();
echo "Tenant {$tenant->getTenantId()} has {$count} organizations\n";
});
}
// Get all companies
$companies = Organization::ofType('company')->get();
// Get all departments
$departments = Organization::ofType('department')->get();
// Get active organizations
$active = Organization::active()->get();
// Get organization tree
$tree = Organization::tree();
// Get children of an organization
$children = $organization->children;
// Get parent organization
$parent = $organization->parent;
// Get all descendants
$descendants = $organization->descendants();
// Get all ancestors
$ancestors = $organization->ancestors();
// Assign primary manager
$organization->update(['manager_id' => $user->id]);
// Assign additional managers with roles
$organization->users()->attach($user->id, ['role' => 'manager']);
$organization->users()->attach($user2->id, ['role' => 'supervisor']);
// Get all managers
$managers = $organization->managers();
use Litepie\Organization\Traits\HasOrganization;
class User extends Authenticatable
{
use HasOrganization;
}
// Get user's organizations
$organizations = $user->organizations;
// Get user's organizations with specific role
$managedOrganizations = $user->organizationsWithRole('manager');
// Check if user belongs to organization
if ($user->belongsToOrganization($organizationId)) {
// User belongs to this organization
}