### Creating Eloquent Models and Migrations with Artisan Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This snippet uses Laravel Artisan commands to generate new Eloquent models (`Owner`, `Patient`, `Treatment`) along with their corresponding database migration files. The `-m` flag ensures that migration files are created simultaneously, streamlining the setup process for database tables. ```bash php artisan make:model Owner -m php artisan make:model Patient -m php artisan make:model Treatment -m ``` -------------------------------- ### Creating a Filament Relation Manager using Artisan Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This command generates a new relation manager file, connecting a parent resource (PatientResource) to its related records (treatments) and specifying a column (description) to display in the relation manager's table. It streamlines the setup process for managing related data within Filament. ```bash php artisan make:filament-relation-manager PatientResource treatments description ``` -------------------------------- ### Implementing MoneyCast Get and Set Methods (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md Defines the `get` and `set` methods for the `MoneyCast` class. The `get` method converts an integer value from the database into a float (dividing by 100) for application use, while the `set` method converts a float back into an integer (multiplying by 100) for database storage, ensuring proper currency precision. ```php public function get($model, string $key, $value, array $attributes): float { // Transform the integer stored in the database into a float. return round(floatval($value) / 100, precision: 2); } public function set($model, string $key, $value, array $attributes): float { // Transform the float into an integer for storage. return round(floatval($value) * 100); } ``` -------------------------------- ### Generating URL to Specific Resource Page (FilamentPHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This example shows how to generate a URL to a specific page within a FilamentPHP resource, such as the 'create' page. The page name is passed as the first argument to the `getUrl()` method, corresponding to the key in the resource's `getPages()` array. ```PHP use App\Filament\Resources\CustomerResource; CustomerResource::getUrl('create'); // /admin/customers/create ``` -------------------------------- ### Installing Laravel Trend Package (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This Bash command installs the `flowframe/laravel-trend` Composer package. This package is recommended by Filament for efficiently populating chart data from Eloquent models by providing convenient methods for aggregating model data over time. ```bash composer require flowframe/laravel-trend ``` -------------------------------- ### Scaffolding Model, Migration, and Factory with Filament Resource (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This command streamlines the scaffolding process by simultaneously generating the Eloquent model, its corresponding database migration, and a factory alongside the Filament resource. This saves time when setting up new features from scratch. ```bash php artisan make:filament-resource Customer --model --migration --factory ``` -------------------------------- ### Creating a Filament Resource (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This command generates a new Filament resource for the App\Models\Customer Eloquent model. It creates the main CustomerResource.php class and associated page classes (List, Create, Edit) in the app/Filament/Resources directory, providing a basic CRUD interface. ```bash php artisan make:filament-resource Customer ``` -------------------------------- ### Filament Resource File Structure (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This output illustrates the directory and file structure created by the make:filament-resource Artisan command. It shows the main resource class and its associated Livewire page components for managing records (Create, Edit, List). ```bash . +-- CustomerResource.php +-- CustomerResource | +-- Pages | | +-- CreateCustomer.php | | +-- EditCustomer.php | | +-- ListCustomers.php ``` -------------------------------- ### Removing Pages from FilamentPHP Resource (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This method defines the available pages for a FilamentPHP resource. To remove a page, its entry must be deleted from this array, in addition to deleting the corresponding page file. This example shows the `Create` page removed, indicating that new records cannot be created directly through this resource's UI. ```PHP public static function getPages(): array { return [ 'index' => Pages\ListCustomers::route('/'), 'edit' => Pages\EditCustomer::route('/{record}/edit'), ]; } ``` -------------------------------- ### Generating a View Page for Filament Resource (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This command includes a dedicated "View" page when generating the Filament resource, in addition to the default List, Create, and Edit pages. The View page allows administrators to inspect individual record details without editing them. ```bash php artisan make:filament-resource Customer --view ``` -------------------------------- ### Generating URL for Resource in Different Panel (FilamentPHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This example illustrates how to generate a URL for a FilamentPHP resource that resides in a different panel. By passing the panel ID to the `panel` argument, the `getUrl()` method correctly constructs the URL for the specified panel, enabling cross-panel navigation. ```PHP use App\Filament\Resources\CustomerResource; CustomerResource::getUrl(panel: 'marketing'); ``` -------------------------------- ### Auto-Generating Forms and Tables for Filament Resource (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This command automatically generates the form and table schema for the Filament resource based on the database columns of the associated Eloquent model. This saves development time by inferring fields and columns from the model's structure. ```bash php artisan make:filament-resource Customer --generate ``` -------------------------------- ### Generating a Simple Filament Resource (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This command creates a simplified Filament resource where records are managed on a single "Manage" page using modals for create, edit, and delete operations. It omits the getRelations() method, as relation managers are not used in simple resources. ```bash php artisan make:filament-resource Customer --simple ``` -------------------------------- ### Defining Dynamic Navigation Label in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet shows how to define a dynamic navigation menu item label for a FilamentPHP resource by implementing the `getNavigationLabel()` method. This allows for localized or conditional labels. ```php public static function getNavigationLabel(): string { return __('filament/resources/customer.navigation_label'); } ``` -------------------------------- ### Generating Filament Chart Widget (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This Bash command utilizes the Artisan CLI to generate a new Filament widget, specifically configured as a chart. The `--chart` flag ensures the widget is set up for chart rendering, prompting the user for the location and desired chart type. ```bash php artisan make:filament-widget TreatmentsChart --chart ``` -------------------------------- ### Defining Dynamic Navigation Icon in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet demonstrates how to set a dynamic navigation icon for a FilamentPHP resource by implementing the `getNavigationIcon()` method. This allows for conditional icon display based on logic. ```php use Illuminate\Contracts\Support\Htmlable; public static function getNavigationIcon(): string | Htmlable | null { return 'heroicon-o-user-group'; } ``` -------------------------------- ### Defining Resource Tables in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet demonstrates how to define the table structure for a FilamentPHP resource using the `table()` method. It includes column definitions, filters, and actions, which are rendered on the resource's list page. ```PHP use Filament\Tables; use Filament\Tables\Table; use Illuminate\Database\Eloquent\Builder; public static function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('name'), Tables\Columns\TextColumn::make('email'), // ... ]) ->filters([ Tables\Filters\Filter::make('verified') ->query(fn (Builder $query): Builder => $query->whereNotNull('email_verified_at')), // ... ]) ->actions([ Tables\Actions\EditAction::make(), ]) ->bulkActions([ Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), ]), ]); } ``` -------------------------------- ### Defining Dynamic Model Label in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet shows how to define a dynamic singular model label for a FilamentPHP resource by implementing the `getModelLabel()` method. This allows for localization or more complex logic. ```php public static function getModelLabel(): string { return __('filament/resources/customer.label'); } ``` -------------------------------- ### Creating a Filament Resource using Artisan Command (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This Artisan command generates a new Filament resource for the specified Eloquent model, creating the necessary PHP files and directory structure for building CRUD interfaces. It's the foundational step for integrating a model with Filament's admin panel. ```bash php artisan make:filament-resource Patient ``` -------------------------------- ### Adding Created At Column to Treatment Table (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md Extends the Filament table configuration to include a `created_at` column. This column displays the timestamp of when the treatment was administered, formatted into a human-readable date and time string using the `dateTime()` method. ```php use FilamentTables; use FilamentTablesTable; public function table(Table $table): Table { return $table ->columns([ TablesColumnsTextColumn::make('description'), TablesColumnsTextColumn::make('price') ->money('EUR') ->sortable(), TablesColumnsTextColumn::make('created_at') ->dateTime(), ]); } ``` -------------------------------- ### Defining Dynamic Navigation Sort Order in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet demonstrates how to set a dynamic navigation item order in FilamentPHP by implementing the `getNavigationSort()` method. This allows for programmatic control over sorting. ```php public static function getNavigationSort(): ?int { return 2; } ``` -------------------------------- ### Defining Dynamic Navigation Group in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet shows how to set a dynamic navigation group label for a FilamentPHP resource by implementing the `getNavigationGroup()` method. This enables localization or conditional grouping. ```php public static function getNavigationGroup(): ?string { return __('filament/navigation.groups.shop'); } ``` -------------------------------- ### Applying MoneyCast to Treatment Model (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md Integrates the `MoneyCast` into the `Treatment` Eloquent model by assigning it to the `price` attribute within the `$casts` array. This ensures that the `price` attribute automatically uses the custom casting logic when retrieved from or saved to the database. ```php use AppCastsMoneyCast; use IlluminateDatabaseEloquentModel; class Treatment extends Model { protected $casts = [ 'price' => MoneyCast::class, ]; // ... } ``` -------------------------------- ### Configuring Treatment Table Columns with Price Formatting (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md Configures the `table` method within a Filament relation manager or resource to define its columns. This snippet adds a `description` text column and a `price` column, which is formatted as `EUR` currency using the `money()` method and made sortable. ```php use FilamentTables; use FilamentTablesTable; public function table(Table $table): Table { return $table ->columns([ TablesColumnsTextColumn::make('description'), TablesColumnsTextColumn::make('price') ->money('EUR') ->sortable(), ]); } ``` -------------------------------- ### Enabling Soft Deletes for Filament Resource (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This command configures the generated Filament resource to support soft-deleted records. It adds functionality to restore, force delete, and filter trashed records within the application's interface, integrating with Laravel's soft delete feature. ```bash php artisan make:filament-resource Customer --soft-deletes ``` -------------------------------- ### Implementing Patient Type Stats Overview Widget (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md Defines the `PatientTypeOverview` widget, extending `StatsOverviewWidget`, to display patient statistics. The `getStats()` method returns an array of `Stat` instances, each representing a count of patients filtered by their `type` (e.g., 'cat', 'dog', 'rabbit'), providing a quick overview on the dashboard. ```php where('type', 'cat')->count()), Stat::make('Dogs', Patient::query()->where('type', 'dog')->count()), Stat::make('Rabbits', Patient::query()->where('type', 'rabbit')->count()), ]; } } ``` -------------------------------- ### Generating URL to Resource List Page (FilamentPHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet demonstrates how to generate a URL to a FilamentPHP resource's default list page using the static `getUrl()` method without any arguments. It simplifies URL construction by abstracting away slug and route naming conventions. ```PHP use App\Filament\Resources\CustomerResource; CustomerResource::getUrl(); // /admin/customers ``` -------------------------------- ### Generating Patient Type Stats Widget (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md Executes an Artisan command to create a new Filament stats overview widget named `PatientTypeOverview`. The `--stats-overview` flag ensures it's configured as a stats widget, designed to display key statistical information on a dashboard. ```bash php artisan make:filament-widget PatientTypeOverview --stats-overview ``` -------------------------------- ### Generating URL to Resource Page with Record Parameter (FilamentPHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet illustrates generating a URL to a resource page that requires a record parameter, like the 'edit' page. The record, which can be an Eloquent model or an ID, is passed as the second argument within an associative array. ```PHP use App\Filament\Resources\CustomerResource; CustomerResource::getUrl('edit', ['record' => $customer]); // /admin/customers/edit/1 ``` -------------------------------- ### Customizing Resource Eloquent Query with Constraint (FilamentPHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet demonstrates how to customize the base Eloquent query for a FilamentPHP resource by overriding the `getEloquentQuery()` method. It applies a `where` clause to filter records, ensuring that all subsequent queries within the resource observe this constraint. ```PHP public static function getEloquentQuery(): Builder { return parent::getEloquentQuery()->where('is_active', true); } ``` -------------------------------- ### Defining Dynamic Navigation Parent Item in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet shows how to set a dynamic parent item label for a FilamentPHP resource by implementing the `getNavigationParentItem()` method. This allows for programmatic control over nested navigation. ```php public static function getNavigationParentItem(): ?string { return __('filament/navigation.groups.shop.items.products'); } ``` -------------------------------- ### Generating URL for Page Action Modal (FilamentPHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet shows how to generate a URL to trigger a page-level action modal, such as a `CreateAction` in the header. The action's default name is passed via the `action` parameter, allowing direct linking to specific page actions. ```PHP use App\Filament\Resources\CustomerResource; use Filament\Actions\CreateAction; CustomerResource::getUrl(parameters: [ 'action' => CreateAction::getDefaultName(), ]); // /admin/customers?action=create ``` -------------------------------- ### Generating URL for Table Action Modal (FilamentPHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This code demonstrates how to generate a URL that triggers a table action modal within a FilamentPHP resource. It passes `tableAction` and `tableActionRecord` as URL parameters to specify the action and the associated record, useful for simple resources with single pages. ```PHP use App\Filament\Resources\CustomerResource; use Filament\Tables\Actions\EditAction; CustomerResource::getUrl(parameters: [ 'tableAction' => EditAction::getDefaultName(), 'tableActionRecord' => $customer, ]); // /admin/customers?tableAction=edit&tableActionRecord=1 ``` -------------------------------- ### Defining Resource Forms in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet illustrates how to define the form structure for a FilamentPHP resource using the `form()` method. It includes `TextInput` components for 'name' and 'email', marking them as required, which are used on both create and edit pages. ```PHP use Filament\Forms; use Filament\Forms\Form; public static function form(Form $form): Form { return $form ->schema([ Forms\Components\TextInput::make('name')->required(), Forms\Components\TextInput::make('email')->email()->required(), // ... ]); } ``` -------------------------------- ### Establishing Eloquent Relationships Between Owner, Patient, and Treatment Models Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This PHP snippet defines the Eloquent relationships between the `Owner`, `Patient`, and `Treatment` models. It establishes a one-to-many relationship where an `Owner` has many `Patients`, a `Patient` belongs to an `Owner` and has many `Treatments`, and a `Treatment` belongs to a `Patient`, enabling easy data navigation. ```php use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; class Owner extends Model { public function patients(): HasMany { return $this->hasMany(Patient::class); } } class Patient extends Model { public function owner(): BelongsTo { return $this->belongsTo(Owner::class); } public function treatments(): HasMany { return $this->hasMany(Treatment::class); } } class Treatment extends Model { public function patient(): BelongsTo { return $this->belongsTo(Patient::class); } } ``` -------------------------------- ### Installing Filament Panel Builder (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/01-installation.md This command sequence installs the Filament Panel Builder package via Composer and then runs the Filament installation artisan command. It sets up the core Filament components and registers a new Laravel service provider for the admin panel. ```bash composer require filament/filament:"^3.3" -W php artisan filament:install --panels ``` -------------------------------- ### Customizing Relation Manager Form and Table (Initial) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This PHP code shows the initial structure of the `form()` and `table()` methods within a Filament relation manager. It defines a simple text input for 'description' in the form and a text column for 'description' in the table, prepopulated by the Artisan command, allowing for further customization of fields and columns. ```php use Filament\Forms; use Filament\Forms\Form; use Filament\Tables; use Filament\Tables\Table; public function form(Form $form): Form { return $form ->schema([ Forms\Components\TextInput::make('description') ->required() ->maxLength(255), ]); } public function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('description'), ]); } ``` -------------------------------- ### Setting Static Navigation Parent Item and Group in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet demonstrates how to nest a navigation item under a parent item using `$navigationParentItem` and specify its group using `$navigationGroup`. Both properties are required for correct identification. ```php protected static ?string $navigationParentItem = 'Products'; protected static ?string $navigationGroup = 'Shop'; ``` -------------------------------- ### Setting Sub-navigation Position in FilamentPHP Resource (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This property controls where the sub-navigation is rendered on the page. By setting `$subNavigationPosition` to `SubNavigationPosition::End`, `SubNavigationPosition::Start`, or `SubNavigationPosition::Top`, developers can customize the layout of the sub-navigation, with `Top` rendering it as tabs. This allows for flexible UI design. ```PHP use Filament\Pages\SubNavigationPosition; protected static SubNavigationPosition $subNavigationPosition = SubNavigationPosition::End; ``` -------------------------------- ### Specifying Custom Model Namespace for Filament Resource (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This command allows specifying a custom PHP namespace for the Eloquent model associated with the Filament resource. The --model-namespace flag ensures Filament can correctly locate the model and read its database schema, requiring double backslashes for the path. ```bash php artisan make:filament-resource Customer --model-namespace=Custom\\Path\\Models ``` -------------------------------- ### Setting Static Navigation Group in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet illustrates how to group navigation items under a static label in FilamentPHP using the `$navigationGroup` property. Items with the same group label will appear together. ```php protected static ?string $navigationGroup = 'Shop'; ``` -------------------------------- ### Defining Dynamic Plural Model Label in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet demonstrates how to define a dynamic plural model label for a FilamentPHP resource by implementing the `getPluralModelLabel()` method. This is useful for custom pluralization rules or localization. ```php public static function getPluralModelLabel(): string { return __('filament/resources/customer.plural_label'); } ``` -------------------------------- ### Defining Database Schemas for Owner, Patient, and Treatment Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This PHP snippet defines the database schemas for the `owners`, `patients`, and `treatments` tables using Laravel's Schema builder. It includes primary keys, various data types, and foreign key constraints with cascade on delete for relationships, ensuring data integrity. ```php // create_owners_table Schema::create('owners', function (Blueprint $table) { $table->id(); $table->string('email'); $table->string('name'); $table->string('phone'); $table->timestamps(); }); // create_patients_table Schema::create('patients', function (Blueprint $table) { $table->id(); $table->date('date_of_birth'); $table->string('name'); $table->foreignId('owner_id')->constrained('owners')->cascadeOnDelete(); $table->string('type'); $table->timestamps(); }); // create_treatments_table Schema::create('treatments', function (Blueprint $table) { $table->id(); $table->string('description'); $table->text('notes')->nullable(); $table->foreignId('patient_id')->constrained('patients')->cascadeOnDelete(); $table->unsignedInteger('price')->nullable(); $table->timestamps(); }); ``` -------------------------------- ### Setting Static Navigation Sort Order in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet shows how to specify the static order of a navigation item in FilamentPHP using the `$navigationSort` property. Lower numbers appear higher in the navigation menu. ```php protected static ?int $navigationSort = 2; ``` -------------------------------- ### Adding Type Select Field to Filament Resource Form (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This PHP snippet integrates a `Select` field for the 'type' attribute into the Filament resource form. It defines a fixed set of options ('cat', 'dog', 'rabbit') using the `options()` method, suitable for predefined choices. ```php use Filament\Forms; use Filament\Forms\Form; public static function form(Form $form): Form { return $form ->schema([ Forms\Components\TextInput::make('name') ->required() ->maxLength(255), Forms\Components\Select::make('type') ->options([ 'cat' => 'Cat', 'dog' => 'Dog', 'rabbit' => 'Rabbit', ]), ]); } ``` -------------------------------- ### Creating MoneyCast Artisan Command (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This command generates a new Laravel custom cast class named `MoneyCast` in the `app/Casts` directory. This cast is essential for handling currency values by transforming them between integer storage in the database and float representation in the application, preventing precision issues. ```bash php artisan make:cast MoneyCast ``` -------------------------------- ### Setting Static Navigation Label in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet demonstrates how to customize the navigation menu item label for a FilamentPHP resource using the `$navigationLabel` static property. By default, the plural label is used. ```php protected static ?string $navigationLabel = 'Mis Clientes'; ``` -------------------------------- ### Setting Static Navigation Icon in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet illustrates how to set a static navigation icon for a FilamentPHP resource using the `$navigationIcon` property. It supports Blade component names, typically Heroicons by default. ```php protected static ?string $navigationIcon = 'heroicon-o-user-group'; ``` -------------------------------- ### Setting Static Model Label in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet demonstrates how to customize the singular model label for a FilamentPHP resource using the `$modelLabel` static property. This label is used throughout the UI for display purposes. ```php protected static ?string $modelLabel = 'cliente'; ``` -------------------------------- ### Configuring Record Sub-navigation in FilamentPHP Resource (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This method defines the sub-navigation items for a specific record within a FilamentPHP resource. It takes a `Page` instance and uses `generateNavigationItems` to create an array of navigation links, typically pointing to different record-related pages like view, edit, or related record managers. This allows users to easily switch between different views of the same record. ```PHP use App\Filament\Resources\CustomerResource\Pages; use Filament\Resources\Pages\Page; public static function getRecordSubNavigation(Page $page): array { return $page->generateNavigationItems([ Pages\ViewCustomer::class, Pages\EditCustomer::class, Pages\EditCustomerContact::class, Pages\ManageCustomerAddresses::class, Pages\ManageCustomerPayments::class, ]); } ``` -------------------------------- ### Skipping Resource Authorization in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet shows how to bypass authorization checks for a FilamentPHP resource by setting the static property `$shouldSkipAuthorization` to `true`. This allows unrestricted access to the resource, ignoring any defined model policies. ```PHP protected static bool $shouldSkipAuthorization = true; ``` -------------------------------- ### Filament Theme Setup Instructions (Text/Vite/PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/12-themes.md These are post-creation instructions for integrating a custom Filament theme. They detail three crucial steps: updating `vite.config.js` to include the theme's CSS input, registering the theme in the panel provider using `->viteTheme()`, and finally compiling the theme with `npm run build`. These steps are essential for the theme to be recognized and applied. ```text ⇂ First, add a new item to the `input` array of `vite.config.js`: `resources/css/filament/admin/theme.css` ⇂ Next, register the theme in the admin panel provider using `->viteTheme('resources/css/filament/admin/theme.css')` ⇂ Finally, run `npm run build` to compile the theme ``` -------------------------------- ### Showing Form Components Conditionally in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet showcases the `visibleOn()` method, a shortcut for displaying a form component only on a specific page or action. The 'password' `TextInput` field is set to be visible exclusively on the 'create' page. ```PHP use Livewire\Component; Forms\Components\TextInput::make('password') ->password() ->required() ->visibleOn('create'), ``` -------------------------------- ### Registering a Relation Manager in Filament Resource Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This PHP snippet demonstrates how to register a newly created relation manager (TreatmentsRelationManager) within the `getRelations()` method of the parent resource (PatientResource). This registration is crucial for Filament to recognize and display the relation manager on the parent resource's edit page. ```php use App\Filament\Resources\PatientResource\RelationManagers; public static function getRelations(): array { return [ RelationManagers\TreatmentsRelationManager::class, ]; } ``` -------------------------------- ### Setting Static Plural Model Label in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet illustrates how to customize the plural model label for a FilamentPHP resource using the `$pluralModelLabel` static property. This label is automatically pluralized from the singular label by default. ```php protected static ?string $pluralModelLabel = 'clientes'; ``` -------------------------------- ### Customizing Resource URL Slug (FilamentPHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet demonstrates how to customize the URL slug for a FilamentPHP resource. By setting the static `$slug` property, developers can define a custom URL segment instead of the default one generated from the resource's name, providing more control over routing. ```PHP protected static ?string $slug = 'pending-orders'; ``` -------------------------------- ### Adding Date of Birth Picker to Filament Resource Form (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This PHP snippet integrates a `DatePicker` field for the 'date_of_birth' attribute into the Filament resource form. It includes `required()` validation and `maxDate(now())` to prevent future dates, ensuring accurate and valid date inputs. ```php use Filament\Forms; use Filament\Forms\Form; public static function form(Form $form): Form { return $form ->schema([ Forms\Components\TextInput::make('name') ->required() ->maxLength(255), Forms\Components\Select::make('type') ->options([ 'cat' => 'Cat', 'dog' => 'Dog', 'rabbit' => 'Rabbit', ]) ->required(), Forms\Components\DatePicker::make('date_of_birth') ->required() ->maxDate(now()), ]); } ``` -------------------------------- ### Creating a Filament User (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/01-installation.md This Artisan command creates a new user account specifically for Filament, prompting for necessary details like email and password. This user can then be used to log into the Filament admin panel. ```bash php artisan make:filament-user ``` -------------------------------- ### Publishing Filament Panel Configuration (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/01-installation.md This command publishes the core configuration file for Filament panels. It's used to customize default settings and integrate Filament more deeply into a Laravel application. This is a prerequisite for modifying Filament's default behavior. ```bash php artisan vendor:publish --tag=filament-config ``` -------------------------------- ### Installing and Running Filament v3 Automated Upgrade Script (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/15-upgrade-guide.md This snippet demonstrates how to install the Filament v3 automated upgrade script using Composer and then execute it. This script handles most breaking changes automatically, simplifying the upgrade process. ```bash composer require filament/upgrade:"^3.2" -W --dev vendor/bin/filament-v3 ``` -------------------------------- ### Hiding Form Components Conditionally in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This example demonstrates using the `hiddenOn()` method to dynamically hide a form component. The 'password' `TextInput` field is configured to be hidden specifically on the 'edit' page, preventing its display during record updates. ```PHP use Livewire\Component; Forms\Components\TextInput::make('password') ->password() ->required() ->hiddenOn('edit'), ``` -------------------------------- ### Optimizing Laravel Application (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/01-installation.md This command optimizes the core Laravel application for production by caching configuration files and routes. It's a standard Laravel optimization step that complements Filament-specific optimizations. ```bash php artisan optimize ``` -------------------------------- ### Adding Name Text Input to Filament Resource Form (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This PHP snippet demonstrates how to add a basic text input field for the 'name' attribute to a Filament resource form. It utilizes `Forms\Components\TextInput::make('name')` within the `schema` array of the `form()` method to define the input. ```php use Filament\Forms; use Filament\Forms\Form; public static function form(Form $form): Form { return $form ->schema([ Forms\Components\TextInput::make('name'), ]); } ``` -------------------------------- ### Populating Filament Chart Data with Laravel Trend (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This PHP method, `getData()`, within a Filament chart widget, is responsible for retrieving and formatting treatment data for chart display. It leverages the `Flowframe\Trend` package to count `Treatment` model instances per month over the past year, mapping the aggregated values and dates into a structure compatible with Chart.js for rendering. ```php use App\Models\Treatment; use Flowframe\Trend\Trend; use Flowframe\Trend\TrendValue; protected function getData(): array { $data = Trend::model(Treatment::class) ->between( start: now()->subYear(), end: now(), ) ->perMonth() ->count(); return [ 'datasets' => [ [ 'label' => 'Treatments', 'data' => $data->map(fn (TrendValue $value) => $value->aggregate), ], ], 'labels' => $data->map(fn (TrendValue $value) => $value->date), ]; } ``` -------------------------------- ### Optimizing Filament for Production (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/01-installation.md This command optimizes Filament for production environments by caching Filament components and Blade icons. It's a shorthand for `php artisan filament:cache-components` and `php artisan icons:cache`, significantly improving panel performance. ```bash php artisan filament:optimize ``` -------------------------------- ### Caching Filament Components (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/01-installation.md This command creates cache files for Filament components (resources, pages, widgets, etc.) in `bootstrap/cache/filament`. It improves performance by reducing file scanning, but should be avoided during active local development. ```bash php artisan filament:cache-components ``` -------------------------------- ### Authenticating User in PHPUnit/Pest TestCase Source: https://github.com/filamentphp/panels/blob/3.x/docs/14-testing.md This `setUp` method ensures that a user is authenticated before each test runs. It calls the parent `setUp` method and then uses `actingAs` to log in a newly created user, which is a prerequisite for accessing protected parts of the Filament application during tests. ```PHP protected function setUp(): void { parent::setUp(); $this->actingAs(User::factory()->create()); } ``` -------------------------------- ### Creating a Filament Theme with Custom Package Manager (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/12-themes.md This command creates a custom theme for a Filament panel, explicitly specifying 'bun' as the package manager for dependency installation. The `--pm` option allows developers to override the default 'npm' behavior, accommodating different project setups or preferences for package management tools. ```bash php artisan make:filament-theme --pm=bun ``` -------------------------------- ### Disabling Mass Assignment Protection in Laravel Models Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This PHP snippet demonstrates how to disable Laravel's mass assignment protection for all Eloquent models by calling `Model::unguard()` within the `boot()` method of `AppServiceProvider.php`. This is done for brevity in the guide, as Filament handles data validation securely. ```php use Illuminate\Database\Eloquent\Model; public function boot(): void { Model::unguard(); } ``` -------------------------------- ### Publishing Filament Panels Translations (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/01-installation.md This command publishes the language files specifically for the Filament Panels package. It allows developers to customize or add new translations for the panel's user interface elements. ```bash php artisan vendor:publish --tag=filament-panels-translations ``` -------------------------------- ### Applying Required Validation to Type Select Field (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This PHP snippet adds the `required()` validation rule to the 'type' select field. This ensures that users must select one of the provided options before the form can be successfully submitted, enforcing data integrity. ```php use Filament\Forms; Forms\Components\Select::make('type') ->options([ 'cat' => 'Cat', 'dog' => 'Dog', 'rabbit' => 'Rabbit', ]) ->required() ``` -------------------------------- ### Enhancing Owner Select Field with Search and Preload (FilamentPHP, PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This snippet demonstrates how to enhance the 'owner_id' select field in a FilamentPHP form. It makes the field 'searchable()' to allow users to filter the list of owners and 'preload()' the first 50 owners for faster access, especially when dealing with a large number of records. The field remains required. ```php use Filament\Forms; Forms\Components\Select::make('owner_id') ->relationship('owner', 'name') ->searchable() ->preload() ->required() ``` -------------------------------- ### Configuring Numeric Price Field with Prefix in Filament Form Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This PHP snippet demonstrates adding a 'price' field to the form, configured as `numeric()` for validation and mobile keyboard optimization. It also applies a currency prefix (e.g., '€') using `prefix('€')` to enhance user experience without affecting the stored value, and sets a maximum value. ```php use Filament\Forms; use Filament\Forms\Form; public function form(Form $form): Form { return $form ->schema([ Forms\Components\TextInput::make('description') ->required() ->maxLength(255) ->columnSpan('full'), Forms\Components\Textarea::make('notes') ->maxLength(65535) ->columnSpan('full'), Forms\Components\TextInput::make('price') ->numeric() ->prefix('€') ->maxValue(42949672.95), ]); } ``` -------------------------------- ### Installing Filament Spark Billing Provider Source: https://github.com/filamentphp/panels/blob/3.x/docs/11-tenancy.md This Bash command installs the Filament Spark billing provider package via Composer. This package is a prerequisite for integrating Laravel Spark's billing functionalities with Filament panels, allowing users to manage subscriptions and billing information. ```Bash composer require filament/spark-billing-provider ``` -------------------------------- ### Disabling All Global Scopes in Resource Query (FilamentPHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This code shows how to disable all global scopes applied to a model within a FilamentPHP resource's query. By calling `withoutGlobalScopes()` on the parent query, it allows access to records that would otherwise be filtered out, such as soft-deleted entries. ```PHP public static function getEloquentQuery(): Builder { return parent::getEloquentQuery()->withoutGlobalScopes(); } ``` -------------------------------- ### Applying Validation Rules to Name Text Input (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This PHP snippet extends the 'name' text input field by adding `required()` and `maxLength(255)` validation rules. These rules ensure that the field is not left empty and its input does not exceed 255 characters, aligning with typical database constraints. ```php use Filament\Forms; Forms\Components\TextInput::make('name') ->required() ->maxLength(255) ``` -------------------------------- ### Making Columns Searchable in FilamentPHP Tables (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This snippet demonstrates how to enable searching on specific columns in a FilamentPHP table. By chaining the `searchable()` method to `TextColumn` instances, users can filter table entries based on the content of the 'name' and 'owner.name' columns. This enhances user experience by providing direct search functionality within the table. ```PHP use Filament\Tables; use Filament\Tables\Table; public static function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('name') ->searchable(), Tables\Columns\TextColumn::make('type'), Tables\Columns\TextColumn::make('date_of_birth'), Tables\Columns\TextColumn::make('owner.name') ->searchable(), ]); } ``` -------------------------------- ### Configuring Patient Table with Text Columns (FilamentPHP, PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This snippet shows how to define columns for the patient table in FilamentPHP. It uses 'TextColumn::make()' to display the patient's name, type, date of birth, and the owner's name. The 'owner.name' syntax demonstrates Filament's dot notation for eager-loading and displaying related data, avoiding the display of less informative ID numbers. ```php use Filament\Tables; use Filament\Tables\Table; public static function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('name'), Tables\Columns\TextColumn::make('type'), Tables\Columns\TextColumn::make('date_of_birth'), Tables\Columns\TextColumn::make('owner.name') ]); } ``` -------------------------------- ### Configuring Patient Form with Owner Select (FilamentPHP, PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This snippet defines the form schema for creating a new patient in FilamentPHP. It includes fields for patient name, type (cat, dog, rabbit), date of birth, and an 'owner_id' select field. The 'owner_id' field uses the 'relationship()' method to load owner names from the related 'Owner' model, making it easy to associate a patient with an existing owner. All fields are marked as required. ```php use Filament\Forms; use Filament\Forms\Form; public static function form(Form $form): Form { return $form ->schema([ Forms\Components\TextInput::make('name') ->required() ->maxLength(255), Forms\Components\Select::make('type') ->options([ 'cat' => 'Cat', 'dog' => 'Dog', 'rabbit' => 'Rabbit' ]) ->required(), Forms\Components\DatePicker::make('date_of_birth') ->required() ->maxDate(now()), Forms\Components\Select::make('owner_id') ->relationship('owner', 'name') ->required() ]); } ``` -------------------------------- ### Setting Record Title Attribute in Filament Resource (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This PHP snippet defines the $recordTitleAttribute property within a Filament resource class. It specifies the Eloquent model column (e.g., 'name' or 'title') that Filament should use to identify individual records, which is crucial for features like global search. ```php protected static ?string $recordTitleAttribute = 'name'; ``` -------------------------------- ### Adding Textarea Field with Full Column Span to Filament Form Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This PHP code extends the form schema to include a 'notes' field using `Textarea::make()`, also configured to span the full width of the form with `columnSpan('full')`. This allows for capturing more detailed, multi-line information within the treatment form. ```php use Filament\Forms; use Filament\Forms\Form; public function form(Form $form): Form { return $form ->schema([ Forms\Components\TextInput::make('description') ->required() ->maxLength(255) ->columnSpan('full'), Forms\Components\Textarea::make('notes') ->maxLength(65535) ->columnSpan('full'), ]); } ``` -------------------------------- ### Configuring Full Domain Tenancy in FilamentPHP (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/11-tenancy.md This example illustrates how to configure FilamentPHP to resolve the entire domain from the tenant model, allowing for structures like `example.com` or `subdomain.example.com`. The `tenantDomain()` method is used with a dynamic domain parameter, requiring the tenant model's `domain` attribute to contain a valid host. ```PHP use App\Models\Team; use Filament\Panel; public function panel(Panel $panel): Panel { return $panel // ... ->tenant(Team::class, slugAttribute: 'domain') ->tenantDomain('{tenant:domain}'); } ``` -------------------------------- ### Manually Upgrading Filament and Clearing Caches (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/01-installation.md These commands demonstrate the manual process for upgrading Filament. First, `composer update` fetches the latest package versions, then `php artisan filament:upgrade` clears Laravel caches and republishes frontend assets, ensuring a clean update. ```bash composer update php artisan filament:upgrade ``` -------------------------------- ### Configuring Composer Post-Autoload Dump Hook for Filament Upgrade (JSON) Source: https://github.com/filamentphp/panels/blob/3.x/docs/01-installation.md This JSON snippet shows how to add the `filament:upgrade` command to Composer's `post-autoload-dump` script. This ensures that after `composer dump-autoload` or `composer update`, Filament's upgrade command is automatically executed to clear caches and republish assets. ```json "post-autoload-dump": [ // ... "@php artisan filament:upgrade" ], ``` -------------------------------- ### Registering a Filament Plugin Using the Fluent `make()` Method in PHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/13-plugins.md This example illustrates how to register a Filament plugin with a panel using the fluent `make()` method. This approach offers a more concise and readable way to instantiate and integrate the plugin compared to direct instantiation. ```PHP use DanHarrin\FilamentBlog\BlogPlugin; use Filament\Panel; public function panel(Panel $panel): Panel { return $panel // ... ->plugin(BlogPlugin::make()); } ``` -------------------------------- ### Testing Create Page Rendering and Routing in PHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/14-testing.md This test confirms that the Create page for `PostResource` can be successfully rendered and accessed. It obtains the 'create' URL for the resource, performs an HTTP GET request, and then asserts that the response indicates success, validating the routing and rendering of the creation form. ```PHP it('can render page', function () { $this->get(PostResource::getUrl('create'))->assertSuccessful(); }); ``` -------------------------------- ### Adding On-the-Fly Owner Creation to Select Field (FilamentPHP, PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This snippet extends the 'owner_id' select field to allow users to create new owners directly from the patient creation form using a modal. The 'createOptionForm()' method embeds a form with 'TextInput' fields for the owner's name, email address (validated as email), and phone number (validated as tel). Custom labels are applied for email and phone fields. ```php use Filament\Forms; Forms\Components\Select::make('owner_id') ->relationship('owner', 'name') ->searchable() ->preload() ->createOptionForm([ Forms\Components\TextInput::make('name') ->required() ->maxLength(255), Forms\Components\TextInput::make('email') ->label('Email address') ->email() ->required() ->maxLength(255), Forms\Components\TextInput::make('phone') ->label('Phone number') ->tel() ->required() ]) ->required() ``` -------------------------------- ### Publishing Dependent Filament Package Translations (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/01-installation.md These commands publish language files for various Filament sub-packages (actions, forms, infolists, notifications, tables, and general Filament translations). This is necessary to ensure full localization across all Filament components used in the application. ```bash php artisan vendor:publish --tag=filament-actions-translations php artisan vendor:publish --tag=filament-forms-translations php artisan vendor:publish --tag=filament-infolists-translations php artisan vendor:publish --tag=filament-notifications-translations php artisan vendor:publish --tag=filament-tables-translations php artisan vendor:publish --tag=filament-translations ``` -------------------------------- ### Filtering Table by Patient Type in FilamentPHP Tables (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/02-getting-started.md This snippet shows how to implement a `SelectFilter` in a FilamentPHP table to allow users to filter records by patient type. The `SelectFilter::make('type')` with predefined options ('cat', 'dog', 'rabbit') adds a dropdown filter, significantly improving the user experience compared to simple searching for categorical data. This filter applies a scope to the Eloquent query, reducing the displayed records. ```PHP use Filament\Tables; use Filament\Tables\Table; public static function table(Table $table): Table { return $table ->columns([ // ... ]) ->filters([ Tables\Filters\SelectFilter::make('type') ->options([ 'cat' => 'Cat', 'dog' => 'Dog', 'rabbit' => 'Rabbit', ]), ]); } ``` -------------------------------- ### Applying Persistent Tenant Middleware in FilamentPHP (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/11-tenancy.md This example demonstrates how to make tenant-aware middleware persistent, ensuring it runs on every request, including Livewire AJAX requests. This is achieved by passing `true` as the second argument to the `tenantMiddleware()` method in the panel configuration. ```PHP use Filament\Panel; public function panel(Panel $panel): Panel { return $panel // ... ->tenantMiddleware([ // ... ], isPersistent: true); } ``` -------------------------------- ### Disabling Model Label Capitalization in FilamentPHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/01-getting-started.md This snippet shows how to disable the automatic capitalization of model labels in FilamentPHP for specific UI elements like page titles and navigation. Set the `$hasTitleCaseModelLabel` property to `false`. ```php protected static bool $hasTitleCaseModelLabel = false; ``` -------------------------------- ### Caching Blade Icons (Bash) Source: https://github.com/filamentphp/panels/blob/3.x/docs/01-installation.md This command caches Blade Icons, which Filament uses, to improve their performance. It's recommended to run this command locally and in deployment scripts for better application responsiveness. ```bash php artisan icons:cache ``` -------------------------------- ### Registering a Widget with a Filament Resource - PHP Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/09-widgets.md This PHP method, `getWidgets()`, is used within a Filament resource class to register one or more widgets that should be associated with that resource. The example shows how to register the `CustomerOverview` widget, making it available for display on resource pages. ```php public static function getWidgets(): array { return [ CustomerResource\Widgets\CustomerOverview::class, ]; } ``` -------------------------------- ### Redirecting to List Page After Creation (PHP) Source: https://github.com/filamentphp/panels/blob/3.x/docs/03-resources/03-creating-records.md This method customizes the URL to which the user is redirected after a record is successfully created. This specific example redirects the user to the resource's index (list) page. ```php protected function getRedirectUrl(): string { return $this->getResource()::getUrl('index'); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.