### Install Filament Subtenant Scope Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md Install the plugin using Composer. This command adds the necessary package to your project. ```bash composer require leek/filament-subtenant-scope ``` -------------------------------- ### Persist Subtenant Selections Across Sessions Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md Configure callbacks to get and set subtenant scope selections, typically stored in a user's settings column. This makes selections sticky across sessions and devices. ```php SubtenantScopingPlugin::make() ->scopes([/* ... */]) ->persistUsing( get: fn ($user, string $key) => $user->settings['sub_tenant_scopes'][$key] ?? null, set: function ($user, string $key, ?int $id): void { $settings = $user->settings ?? []; $settings['sub_tenant_scopes'][$key] = $id; $user->settings = $settings; $user->saveQuietly(); }, ); ``` -------------------------------- ### Run Tests Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md Execute the project's tests using the Composer command. This is a standard way to ensure the codebase is functioning as expected. ```bash composer test ``` -------------------------------- ### Publish Dropdown Blade View Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md Publish the plugin's dropdown blade view using the provided Artisan command. This allows for customization of the dropdown's appearance and behavior. ```bash php artisan vendor:publish --tag=filament-subtenant-scope-views ``` -------------------------------- ### Configure Multiple Subtenant Scopes Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md Define multiple subtenant scopes, each with a unique key, label, resource, and foreign key. This allows users to switch between different hierarchical scopes. ```php SubtenantScopingPlugin::make() ->scopes([ SubtenantScope::make('region', 'Region', Region::class, 'region_id'), SubtenantScope::make('location', 'Location', Location::class, 'location_id'), ]); ``` -------------------------------- ### Opt Resources into Multiple Scopes Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md Configure individual Filament resources to support specific subtenant scopes by defining the static `$subTenantScopes` property. Map the scope key to the relevant foreign key column. ```php protected static array $subTenantScopes = [ 'region' => 'region_id', 'location' => 'location_id', ]; ``` -------------------------------- ### Opt Resource into Subtenant Scope Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md Add the `HasSubtenantScopes` trait to your resource and map scope keys to FK columns. This automatically filters all queries on the model. ```php use Filament\Resources\Resource; use Leek\FilamentSubtenantScope\Concerns\HasSubtenantScopes; class AppointmentResource extends Resource { use HasSubtenantScopes; /** @var array */ protected static array $subTenantScopes = [ 'service_area' => 'service_area_id', ]; } ``` -------------------------------- ### Register and Configure Subtenant Scopes in Panel Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md Register the SubtenantScopingPlugin with your Filament panel and define the available scopes. Each scope requires a unique key, a display label, the associated model class, and the foreign key column. ```php use Filament\Panel; use Leek\FilamentSubtenantScope\SubtenantScope; use Leek\FilamentSubtenantScope\SubtenantScopingPlugin; use App\Models\ServiceArea; public function panel(Panel $panel): Panel { return $panel // ... ->plugin( SubtenantScopingPlugin::make() ->scopes([ SubtenantScope::make('service_area', 'Service Area', ServiceArea::class, 'service_area_id') ->icon('heroicon-o-map-pin') ->labelAttribute('name') ->optionsQuery(fn ($user) => ServiceArea::query() ->where('company_id', $user->company_id) ->where('is_active', true) ->orderBy('name')), ]), ); } ``` -------------------------------- ### Configure Panel Theme for Styles Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md Include the plugin's blade utility classes in your panel theme's CSS compilation. This ensures responsive utilities are correctly processed. ```css @import '../../../../vendor/filament/filament/resources/css/theme.css'; @source '../../../../vendor/leek/filament-subtenant-scope/resources/views/**/*.blade.php'; ``` -------------------------------- ### Customize Dropdown Rendering with Render Hook Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md Override the default rendering location of the subtenant scope dropdown by specifying a Filament render hook. Ensure the necessary use statement is included. ```php use Filament\View\PanelsRenderHook; SubtenantScopingPlugin::make() ->scopes([/* ... */]) ->renderHook(PanelsRenderHook::TOPBAR_END); ``` -------------------------------- ### Custom Join Logic for Subtenant Scope Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md If the foreign key is not directly on the model, pass `null` for the key and define a static method `scopeSubTenant{Key}` to handle custom query logic. ```php class ClientProfileResource extends Resource { use HasSubtenantScopes; protected static array $subTenantScopes = ['service_area' => null]; public static function scopeSubTenantServiceArea(Builder $query, int $id): void { $query->where(function ($q) use ($id) { $q->where('primary_service_area_id', $id) ->orWhereHas('serviceAreas', fn ($q) => $q->where('service_areas.id', $id)); }); } } ``` -------------------------------- ### Disable Built-in Dropdown Rendering Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md Disable the plugin's default dropdown rendering to embed the Livewire component manually. This provides full control over where the selector appears. ```php SubtenantScopingPlugin::make() ->scopes([/* ... */]) ->withoutDropdown(); ``` -------------------------------- ### Listen for Sub-Scope Changes Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md Use this JavaScript snippet to listen for the 'sub-scope-changed' event dispatched by the Livewire component. This event is triggered after a selection is made and can be used to react to scope changes. ```javascript Livewire.on('sub-scope-changed', ({ scopeKey, value }) => { // ... }); ``` -------------------------------- ### Embed Subtenant Scope Selector Manually Source: https://github.com/leek/filament-subtenant-scope/blob/main/README.md Manually embed the Livewire subtenant scope selector component in your Blade views. This is used when the built-in dropdown rendering is disabled. ```blade @livewire(\'Leek\FilamentSubtenantScope\Livewire\SubtenantScopeSelector::class) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.