main

filamentphp/demo

Last updated at: 29/12/2023 09:43

AddressesRelationManager.php

TLDR

The AddressesRelationManager.php file is a part of the Demo Projects project and is located in the app/Filament/Resources/Shop/CustomerResource/RelationManagers directory. It contains the AddressesRelationManager class, which extends the RelationManager class. The class provides methods for creating a form and a table for managing addresses related to a customer.

Methods

form

This method takes a Form object as a parameter and returns the same object with additional components added to its schema. The added components include text inputs for street, zip code, city, and state, as well as a searchable select input for country.

table

This method takes a Table object as a parameter and returns the same object with columns, filters, header actions, actions, and grouped bulk actions configured. The columns include text columns for street, zip code, and city, and a formatted text column for country. The formatted column displays the country name based on its ID.

Classes

AddressesRelationManager

The AddressesRelationManager class is a subclass of the RelationManager class. It provides methods for creating a form and a table for managing addresses related to a customer. The form method adds text inputs for street, zip code, city, and state, as well as a searchable select input for country. The table method configures columns, filters, header actions, actions, and grouped bulk actions for displaying and managing address data.

<?php

namespace App\Filament\Resources\Shop\CustomerResource\RelationManagers;

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Squire\Models\Country;

class AddressesRelationManager extends RelationManager
{
    protected static string $relationship = 'addresses';

    protected static ?string $recordTitleAttribute = 'full_address';

    public function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('street'),

                Forms\Components\TextInput::make('zip'),

                Forms\Components\TextInput::make('city'),

                Forms\Components\TextInput::make('state'),

                Forms\Components\Select::make('country')
                    ->searchable()
                    ->getSearchResultsUsing(fn (string $query) => Country::where('name', 'like', "%{$query}%")->pluck('name', 'id'))
                    ->getOptionLabelUsing(fn ($value): ?string => Country::find($value)?->getAttribute('name')),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('street'),

                Tables\Columns\TextColumn::make('zip'),

                Tables\Columns\TextColumn::make('city'),

                Tables\Columns\TextColumn::make('country')
                    ->formatStateUsing(fn ($state): ?string => Country::find($state)?->name ?? null),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\AttachAction::make(),
                Tables\Actions\CreateAction::make(),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DetachAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->groupedBulkActions([
                Tables\Actions\DetachBulkAction::make(),
                Tables\Actions\DeleteBulkAction::make(),
            ]);
    }
}