1- # : package_description
1+ # laravel-setanjo - Multi-Tenant Laravel Settings Package
22
3- [ ![ Latest Version on Packagist] ( https://img.shields.io/packagist/v/:vendor_slug/:package_slug.svg?style=flat-square )] ( https://packagist.org/packages/:vendor_slug/:package_slug )
4- [ ![ GitHub Tests Action Status] ( https://img.shields.io/github/actions/workflow/status/:vendor_slug/:package_slug/run-tests.yml?branch=main&label=tests&style=flat-square )] ( https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3Arun-tests+branch%3Amain )
5- [ ![ GitHub Code Style Action Status] ( https://img.shields.io/github/actions/workflow/status/:vendor_slug/:package_slug/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square )] ( https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain )
6- [ ![ Total Downloads] ( https://img.shields.io/packagist/dt/:vendor_slug/:package_slug.svg?style=flat-square )] ( https://packagist.org/packages/:vendor_slug/:package_slug )
7- <!-- delete-->
8- ---
9- This repo can be used to scaffold a Laravel package. Follow these steps to get started:
3+ [ ![ Latest Version on Packagist] ( https://img.shields.io/packagist/v/ahs12/laravel-setanjo.svg?style=flat-square )] ( https://packagist.org/packages/ahs12/laravel-setanjo )
4+ [ ![ GitHub Tests Action Status] ( https://img.shields.io/github/workflow/status/ahs12/laravel-setanjo/run-tests?label=tests )] ( https://github.com/ahs12/laravel-setanjo/actions?query=workflow%3Arun-tests+branch%3Amain )
5+ [ ![ GitHub Code Style Action Status] ( https://img.shields.io/github/workflow/status/ahs12/laravel-setanjo/Fix%20PHP%20code%20style%20issues?label=code%20style )] ( https://github.com/ahs12/laravel-setanjo/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain )
6+ [ ![ Total Downloads] ( https://img.shields.io/packagist/dt/ahs12/laravel-setanjo.svg?style=flat-square )] ( https://packagist.org/packages/ahs12/laravel-setanjo )
107
11- 1 . Press the "Use this template" button at the top of this repo to create a new repo with the contents of this skeleton.
12- 2 . Run "php ./configure.php" to run a script that will replace all placeholders throughout all the files.
13- 3 . Have fun creating your package.
14- 4 . If you need help creating a package, consider picking up our <a href =" https://laravelpackage.training " >Laravel Package Training</a > video course.
15- ---
16- <!-- /delete-->
17- This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
8+ A powerful multi-tenant Laravel settings package that supports both strict and polymorphic tenancy modes with caching, validation, and a clean API.
189
19- ## Support us
10+ ## Features
2011
21- [ <img src =" https://github-ads.s3.eu-central-1.amazonaws.com/:package_name.jpg?t=1 " width =" 419px " />] ( https://spatie.be/github-ad-click/:package_name )
22-
23- We invest a lot of resources into creating [ best in class open source packages] ( https://spatie.be/open-source ) . You can support us by [ buying one of our paid products] ( https://spatie.be/open-source/support-us ) .
24-
25- We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [ our contact page] ( https://spatie.be/about-us ) . We publish all received postcards on [ our virtual postcard wall] ( https://spatie.be/open-source/postcards ) .
12+ - 🏢 ** Multi-Tenant Support** : Both strict and polymorphic tenancy modes
13+ - 🗃️ ** Polymorphic Storage** : Store settings for any model type
14+ - 🏛️ ** Global Settings** : Settings without any tenant scope
15+ - ⚡ ** Caching** : Optional caching with configurable cache store
16+ - 🔒 ** Validation** : Validate tenant models and prevent unauthorized access
17+ - 📦 ** Clean API** : Simple, intuitive API inspired by popular packages
18+ - 🧪 ** Fully Tested** : Comprehensive test suite included
19+ - ✅ ** Type Safety** : Automatic type detection and conversion
2620
2721## Installation
2822
29- You can install the package via composer :
23+ Install the package via Composer :
3024
3125``` bash
32- composer require :vendor_slug/:package_slug
26+ composer require ahs12/laravel-setanjo
3327```
3428
35- You can publish and run the migrations with :
29+ Publish and run the migrations:
3630
3731``` bash
38- php artisan vendor:publish --tag=" :package_slug -migrations"
32+ php artisan vendor:publish --tag=" laravel-setanjo -migrations"
3933php artisan migrate
4034```
4135
42- You can publish the config file with :
36+ Optionally, publish the config file:
4337
4438``` bash
45- php artisan vendor:publish --tag=" :package_slug-config"
39+ php artisan vendor:publish --tag=" laravel-setanjo-config"
40+ ```
41+
42+ ## Quick Start
43+
44+ ### Global Settings
45+
46+ ``` php
47+ use Ahs12\laravel-setanjo\Facades\Settings;
48+
49+ // Set a global setting
50+ Settings::set('app_name', 'My Application');
51+
52+ // Get a setting with default fallback
53+ $appName = Settings::get('app_name', 'Default App Name');
54+
55+ // Check if setting exists
56+ if (Settings::has('maintenance_mode')) {
57+ // Do something
58+ }
59+ ```
60+
61+ ### Tenant-Specific Settings
62+
63+ ``` php
64+ // For a specific model instance
65+ $company = Company::find(1);
66+ Settings::for($company)->set('company_name', 'Acme Corp');
67+ $companyName = Settings::for($company)->get('company_name');
68+
69+ // Using the model macro (if enabled)
70+ $company->settings()->set('timezone', 'America/New_York');
71+ $timezone = $company->settings()->get('timezone', 'UTC');
72+
73+ // For tenant by ID (useful in polymorphic mode)
74+ Settings::forTenantId(1, Company::class)->set('setting', 'value');
75+ ```
76+
77+ ### Data Types
78+
79+ laravel-setanjo automatically detects and handles different data types:
80+
81+ ``` php
82+ Settings::set('string_setting', 'Hello World');
83+ Settings::set('boolean_setting', true);
84+ Settings::set('integer_setting', 42);
85+ Settings::set('float_setting', 3.14);
86+ Settings::set('array_setting', ['key' => 'value']);
87+
88+ // Values are automatically cast to their original types when retrieved
89+ $bool = Settings::get('boolean_setting'); // Returns actual boolean true
90+ $array = Settings::get('array_setting'); // Returns actual array
91+ ```
92+
93+ ## Configuration
94+
95+ The package is highly configurable. Here are the key configuration options:
96+
97+ ### Tenancy Modes
98+
99+ #### Polymorphic Mode (Default)
100+ Allows multiple model types to have settings:
101+
102+ ``` php
103+ // config/laravel-setanjo.php
104+ 'tenancy_mode' => 'polymorphic',
105+ 'allowed_tenant_models' => [
106+ App\Models\Company::class,
107+ App\Models\User::class,
108+ App\Models\Branch::class,
109+ ],
110+ ```
111+
112+ #### Strict Mode
113+ Restricts to a single tenant model type:
114+
115+ ``` php
116+ // config/laravel-setanjo.php
117+ 'tenancy_mode' => 'strict',
118+ 'strict_tenant_model' => App\Models\Company::class,
119+ ```
120+
121+ ### Caching
122+
123+ Enable caching for better performance:
124+
125+ ``` php
126+ // config/laravel-setanjo.php
127+ 'cache' => [
128+ 'enabled' => true,
129+ 'store' => null, // Use default cache store
130+ 'prefix' => 'laravel-setanjo',
131+ 'ttl' => 3600, // 1 hour
132+ ],
46133```
47134
48- This is the contents of the published config file:
135+ ### Default Settings
136+
137+ Define default settings that can be installed via Artisan command:
138+
139+ ``` php
140+ // config/laravel-setanjo.php
141+ 'defaults' => [
142+ 'app_name' => [
143+ 'value' => 'My Laravel App',
144+ 'description' => 'Application name displayed to users',
145+ ],
146+ 'maintenance_mode' => [
147+ 'value' => false,
148+ 'description' => 'Enable maintenance mode',
149+ ],
150+ ],
151+ ```
152+
153+ ## Advanced Usage
154+
155+ ### Multiple Operations
156+
157+ ``` php
158+ // Chain multiple operations
159+ Settings::for($company)
160+ ->set('name', 'Acme Corp')
161+ ->set('timezone', 'America/New_York')
162+ ->set('currency', 'USD');
163+
164+ // Get all settings for a tenant
165+ $allSettings = Settings::for($company)->all();
166+
167+ // Remove specific setting
168+ Settings::for($company)->forget('old_setting');
169+
170+ // Clear all settings for tenant
171+ Settings::for($company)->flush();
172+ ```
173+
174+ ### Working with Different Tenants
175+
176+ ``` php
177+ $company = Company::find(1);
178+ $user = User::find(1);
179+
180+ // Each tenant has isolated settings
181+ Settings::for($company)->set('theme', 'dark');
182+ Settings::for($user)->set('theme', 'light');
183+
184+ // Settings don't interfere with each other
185+ echo Settings::for($company)->get('theme'); // 'dark'
186+ echo Settings::for($user)->get('theme'); // 'light'
187+ ```
188+
189+ ### Validation
190+
191+ The package can validate tenant models:
49192
50193``` php
51- return [
52- ];
194+ // This will throw InvalidTenantException if User is not in allowed_tenant_models
195+ Settings::for($user)->set('setting', 'value');
196+
197+ // Disable validation if needed
198+ // config/laravel-setanjo.php
199+ 'validation' => [
200+ 'enabled' => false,
201+ 'throw_exceptions' => false,
202+ ],
203+ ```
204+
205+ ## Artisan Commands
206+
207+ ### Install Default Settings
208+
209+ ``` bash
210+ php artisan laravel-setanjo:install-defaults
53211```
54212
55- Optionally, you can publish the views using
213+ Install with force (overwrites existing):
56214
57215``` bash
58- php artisan vendor:publish --tag= " :package_slug-views "
216+ php artisan laravel-setanjo:install-defaults --force
59217```
60218
61- ## Usage
219+ ### Clear Settings Cache
220+
221+ ``` bash
222+ php artisan laravel-setanjo:clear-cache
223+ ```
224+
225+ ## Database Schema
226+
227+ The package creates a ` settings ` table with the following structure:
62228
63229``` php
64- $variable = new VendorName\Skeleton();
65- echo $variable->echoPhrase('Hello, VendorName!');
230+ Schema::create('settings', function (Blueprint $table) {
231+ $table->id();
232+ $table->string('key');
233+ $table->longText('value')->nullable();
234+ $table->text('description')->nullable();
235+ $table->string('type')->default('string');
236+ $table->nullableMorphs('tenantable'); // tenantable_type, tenantable_id
237+ $table->timestamps();
238+
239+ // Indexes for performance
240+ $table->index(['key']);
241+ $table->index(['tenantable_type', 'tenantable_id']);
242+ $table->unique(['key', 'tenantable_type', 'tenantable_id']);
243+ });
66244```
67245
246+ ## Performance Considerations
247+
248+ - ** Caching** : Enable caching in production for better performance
249+ - ** Indexing** : The package automatically creates database indexes for optimal queries
250+ - ** Lazy Loading** : Settings are loaded only when needed and cached in memory
251+ - ** Bulk Operations** : Use ` all() ` method instead of multiple ` get() ` calls when possible
252+
68253## Testing
69254
255+ Run the test suite:
256+
70257``` bash
71258composer test
72259```
73260
261+ Run tests with coverage:
262+
263+ ``` bash
264+ composer test-coverage
265+ ```
266+
74267## Changelog
75268
76269Please see [ CHANGELOG] ( CHANGELOG.md ) for more information on what has changed recently.
@@ -85,7 +278,7 @@ Please review [our security policy](../../security/policy) on how to report secu
85278
86279## Credits
87280
88- - [ : author_name ] ( https://github.com/:author_username )
281+ - [ Your Name ] ( https://github.com/yourusername )
89282- [ All Contributors] ( ../../contributors )
90283
91284## License
0 commit comments