Aldo De Cillis
Aldo De Cillis

Reputation: 23

Filament PHP: Showing RelationManager Table through Action

Is there any way to show the table that appears on the bottom of the edit page when I select an Event in my EventResource? I have a ParticipantRelationManager in the EventResource class and it works just fine, I just wanted to display the table in a modal from the List page.

To do so I was trying to create a custom action that triggers a modal in which I wanted to show the table:

Tables\Actions\Action::make('ShowParticipants')
                    ->action(fn (Event $record) => $record->participants())
                    ->modalContent(view('filament.pages.view-participants')),

and then in the public static function getPages() I returned:

'view-participants' => Pages\ViewParticipants::route('/{record}/view-participants'),

I created a class called ViewParticipants.php:

namespace App\Filament\Resources\EventResource\Pages;

use Filament\Resources\Pages\Page;
use App\Models\Event;
use Livewire\Component;

class ViewParticipants extends Page
{
    public static string $view = 'filament.pages.view-participants';

    public function viewData(array $data): array
    {
        $event = Event::findOrFail($data['record']);

        return [
            'participants' => $event->participants,
        ];
    }
}

and a blade view:

<x-filament::page>


@dd('Hello')


</x-filament::page>

I can access the page through the modal, so the @dd('Hello') is shown but I don't know how to retrieve the data from the relation manager.

Thank you so much.

Upvotes: 2

Views: 2626

Answers (1)

Mitesh Rathod
Mitesh Rathod

Reputation: 1059

You can create a new widget as a table listing.

and extends with TableWidget so in your code, it will be

use Filament\Widgets\TableWidget as PageWidget;

class YourWidgetClass extends PageWidget
{

}

Now, register this widget in your ViewParticipants class using

protected function getHeaderWidgets(): array
{
    return [
        YourWidgetClass::class,
    ];
}

You can add widgets in the header 'getHeaderWidgets()' and footer 'getFooterWidgets()' page

You can also customize the table query using the overriding Filament\Widgets\TableWidget::getTableQuery() in your widget class.

Hope, this will help.

Happy coding!..

Upvotes: 1

Related Questions