1. Go to this page and download the library: Download litepie/masters 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/ */
litepie / masters example snippets
// Single database with tenant_id column (default)
'tenancy_strategy' => 'single_db',
// Multiple databases per tenant
'tenancy_strategy' => 'multi_db',
// Schema-based separation
'tenancy_strategy' => 'schema',
use Litepie\Masters\Facades\Masters;
// Set tenant context
Masters::setTenant('tenant_123');
// Get all countries
$countries = Masters::get('countries');
// Create new master data
$country = Masters::create('countries', [
'name' => 'United States',
'code' => 'US',
'iso_code' => 'USA',
'is_active' => true
]);
// Update master data
Masters::update('countries', $country->id, [
'name' => 'United States of America'
]);
// Search master data
$results = Masters::search('countries', 'United');
use Litepie\Masters\Models\MasterData;
use Litepie\Masters\Models\MasterType;
// Get countries with states
$countries = MasterData::ofType('countries')
->with('children')
->active()
->get();
// Create hierarchical data
$usa = MasterData::ofType('countries')->where('code', 'US')->first();
$california = MasterData::create([
'master_type_id' => MasterType::where('slug', 'states')->first()->id,
'name' => 'California',
'code' => 'CA',
'parent_id' => $usa->id,
'is_active' => true
]);
use Litepie\Masters\Facades\Masters;
// Set current tenant
Masters::setTenant('tenant_123');
// Get current tenant
$currentTenant = Masters::getCurrentTenant();
// Execute in tenant context
Masters::runForTenant('tenant_456', function() {
$data = Masters::get('categories');
// This will only get categories for tenant_456
});
// Clear tenant context
Masters::setTenant(null);
// Global data (available to all tenants)
$countries = MasterData::ofType('countries')->global()->get();
// Tenant-specific data
$categories = MasterData::ofType('categories')->tenant('tenant_123')->get();
// Both global and tenant data
$currencies = MasterData::ofType('currencies')->tenantOrGlobal()->get();
// Get full tree structure
$categoryTree = Masters::getHierarchical('categories');
// Get children of specific parent
$subCategories = Masters::getHierarchical('categories', $parentId);
// Get path of an item
$category = MasterData::find(123);
echo $category->getPath(); // "Electronics > Computers > Laptops"
echo $category->getPath(' / '); // "Electronics / Computers / Laptops"
// Check relationships
if ($category->hasChildren()) {
$subcategories = $category->children;
}
if (!$category->isRoot()) {
$parent = $category->parent;
}
// Get all ancestors
$ancestors = $category->ancestors();
// Get all descendants
$descendants = $category->descendants;
bash
# Install package and create default master types
php artisan masters:install
# Seed sample data (optional)
php artisan masters:seed
bash
# Install package and create default types
php artisan masters:install
# Force reinstall
php artisan masters:install --force
# Seed all sample data
php artisan masters:seed
# Seed specific type
php artisan masters:seed countries
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.