#ManyToMany Relationship Attach Action on Custom Table

3 messages · Page 1 of 1 (latest)

frail shoal
#
public function table(Table $table): Table
    {
        $merchant = MerchantResolver::resolveMerchant();

        return $table
            ->query($merchant->paymentChannels()->getQuery())
            ->columns([
                TextColumn::make('name'),
                TextColumn::make('code'),
            ])
            ->actions([
                DetachAction::make(),
            ])
            ->headerActions([
                AttachAction::make(),
            ]);
    }

Is it possible to make AttachAction work? I know I can create my own action button but just wanna know if I can use the default AttachAction to make things cleaner

fierce pagodaBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

frail shoal
#

Fixed it with this

public function table(Table $table): Table
    {
        $merchant = MerchantResolver::resolveMerchant();

        return $table
            ->relationship(fn (): BelongsToMany => $merchant->paymentChannels())
            ->inverseRelationship('merchants')
            ->columns([
                TextColumn::make('name'),
                TextColumn::make('code'),
            ])
            ->actions([
                DetachAction::make(),
            ])
            ->headerActions([
                AttachAction::make()
                    ->recordTitle(fn (\App\Features\PaymentChannel\Models\PaymentChannel $record): string => $record->name),
            ]);
    }