Filament TextInput morphMany

mail@pastecode.io avatar
unknown
php
19 days ago
3.8 kB
5
Indexable
Never
// INVITATION MODEL

class Invitation extends BaseModel
{
    use HasFactory;

    protected $fillable = [
        'invited_by ',
        'action_id',
        'company_id',
        'key',
        'email',
        'first_name',
        'last_name',
        'phone',
        'completed_at',
        'consent_data_processing',
    ];
    
    public function company()
    {
        return $this->belongsTo(Company::class, 'company_id');
    }
}

// COMPANY MODEL
class Company extends BaseModel implements CompanyContract
{
    public function addresses()
    {
        return $this->morphMany(app('Koszala\Addresses\Contracts\Address'), 'addressable');
    }
}

// ADDRESS MODEL

class Address extends BaseModel
{
    protected $fillable = [
    'addressable_type',
    'addressable_id',
    'type',
    'street',
    'building',
    'local',
    'post_code',
    'city',
    'province',
    'county',
    'commune'
    ];
    
    public function addressable()
    {
        return $this->morphTo();
    }
}

// INVITATION RESOURCE
class InvitationResource extends Resource
{
    protected static ?string $model = Invitation::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make()
                    ->columns([
                        'default' => 2,
                    ])
                    ->schema([
                        Forms\Components\Select::make('company_id')
                            ->relationship('company', 'name')
                            ->searchable(['name', 'sap'])
                            ->getOptionLabelFromRecordUsing(function (Model $record) {
                                return "{$record->sap}: {$record->name} ({$record->address_string})";
                            })
                            ->preload()
                            ->live(true, '250ms')
                            ->editOptionForm([
                                Forms\Components\TextInput::make('name')
                                    ->required(),
                                
                                Fieldset::make('Addresses')
                                    ->relationship('addressess')
                                    ->schema([
                                        TextInput::make('street')
                                            ->required(),
                                        TextInput::make('post_code')
                                            ->required(),
                                        TextInput::make('city')
                                            ->required(),
                                    ])
                            ]),
                        Forms\Components\Select::make('action_id')
                            ->relationship('action', 'name')
                            ->searchable(['name'])
                            ->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->name} ({$record->start_at} - {$record->end_at})")
                            ->preload(),
                    ]),
                Forms\Components\Section::make()
                    ->columns([
                        'default' => 2,
                    ])
                    ->schema([
                        TextInput::make('first_name')
                            ->required(),
                        TextInput::make('last_name')
                            ->required(),
                        TextInput::make('phone')
                            ->required(),
                        TextInput::make('email')
                            ->required()
                    ])
            ]);
    }
}
Leave a Comment