Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrishanson's avatar

Calling other resources create actions

I have managed to get what I want working but I don't feel like it's the right way to do it.

I have a Checkin resource and Payments resource, on the checkin list I have an action "Record Payment", this shows a modal with the form I pull from my payment model and it works but it feels messy, I don't want to handle this data mutation in another resources table actions array.

Example work in progress:

Tables\Actions\CreateAction::make('pay')
                    ->mutateFormDataUsing(function(Checkin $checkin, array $data) {
                        $data['gym_id'] = Filament::getTenant()->id;
                        $data['user_id'] = $checkin->user->id;
                        return $data;
                    })
                    ->model(Payment::class)
                    ->form(form: function(Checkin $checkin) {
                        return Payment::getForm($checkin->user->id);
                    })
                    ->label('Record Payment')
                    ->modelLabel('Record Payment')

I have been trying to find how I can call another resources CreateAction, am I missing something key with Filament or is this how it should be done?

0 likes
3 replies
LaryAI's avatar
Level 58

It seems like you are trying to integrate actions across different resources in a Laravel application using the Filament Admin panel. Your current approach involves directly manipulating the form data within the CreateAction of another resource, which can indeed feel a bit messy and tightly coupled.

A cleaner approach might be to use Filament's ability to handle relationships and custom actions more explicitly. Instead of mutating the form data within the CreateAction, you could define a custom action in your Checkin resource that handles the creation of a Payment more transparently.

Here’s a step-by-step solution to refactor your current implementation:

  1. Define a Custom Action in Checkin Resource: Create a custom action within your Checkin resource. This action will handle the creation of the Payment record.

  2. Use Filament Forms to Handle Payment Data: Utilize Filament's form builder to create a form specifically for the payment data within the custom action.

  3. Trigger Payment Creation from Checkin Resource: When the action is triggered, use the form data to create a new Payment record.

Here is how you could implement this:

use Filament\Resources\Tables\Actions\Action;
use Filament\Forms;
use App\Models\Checkin;
use App\Models\Payment;

// In your Checkin resource

public static function getTableActions(): array
{
    return [
        Action::make('recordPayment')
            ->label('Record Payment')
            ->form([
                Forms\Components\TextInput::make('amount')
                    ->label('Payment Amount')
                    ->required(),
                // Add other payment fields as needed
            ])
            ->action(function (Checkin $record, array $data) {
                Payment::create([
                    'gym_id' => Filament::getTenant()->id,
                    'user_id' => $record->user->id,
                    'amount' => $data['amount'],
                    // Map other fields accordingly
                ]);
            }),
    ];
}

This approach has several benefits:

  • Decoupling: The Checkin resource does not directly manipulate the Payment model's internals. It just triggers the creation based on the provided data.
  • Clarity and Maintainability: It’s clearer what happens when the action is triggered, and it’s easier to maintain or modify the behavior.
  • Reusability: The custom action can be easily adjusted or extended for other similar functionalities.

This solution should provide a cleaner and more maintainable way to handle cross-resource actions within your Filament application.

chrishanson's avatar

@LaryAI the problem is I want to create the code once and use it in multiple places, I also want to use this on the Payment resources page in a modal too, I have looked at the relationship managers too but don't think these can help what I want.

martinbean's avatar

@chrishanson

This reply was automagically generated by our local A.I. Think of it as a one-off attempt to instantly solve your problem. It will not respond to further replies.

1 like

Please or to participate in this conversation.