How can I create a list of users loaded onto an event in Filament?

24 views Asked by At

my first post on here.

I'm trying to create a list of users who are loaded onto an event within a filament resource.

In my User Model I have

public function eventUsers(): BelongsToMany
    {
        return $this->belongsToMany(Event::class, 'event_users', 'event_id', 'user_id')->withTimestamps();
    }

In my Event Model I have

public function eventUser() : BelongsToMany
    {
        return $this->belongsToMany(User::class, 'event_users', 'event_id', 'user_id')->withTimestamps();
    }

Clearly there is a pivot table event_users.

I've created a custom page and view:

The custom page

<?php

namespace App\Filament\User\Resources\EventResource\Pages;

use App\Filament\User\Resources\EventResource;
use App\Models\Event;
use App\Models\EventUser;
use App\Models\User;
use Filament\Resources\Pages\Page;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Concerns\InteractsWithTable;


class EventAttendees extends Page implements HasTable
{
    use InteractsWithTable;

    protected static string $resource = EventResource::class;

    protected static string $view = 'filament.user.resources.event-resource.pages.event-attendees';

    public User $user;
    /**
     * @throws \Exception
     */
    public static function table(Table $table): Table
    {
        return $table
            ->query(??????)**<-stuck here**
            ->columns([
                TextColumn::make('name'),
                TextColumn::make('email'),
            ]);
    }

}

Custom blade view

<x-filament-panels::page>

    {{ $this->table }}

</x-filament-panels::page>

In the resource I have an action button

Tables\Actions\Action::make('EventAttendees')
                        ->label('Event Users')
                        ->icon('heroicon-o-printer')
                        ->url(function ($record) {
                            return EventAttendees::getUrl(['user_id' => $record->id]);
                        }),
->recordUrl(function ($record) {
        return EventAttendees::getUrl(['user_id' => $record->id]);

I have imported the page as required

public static function getPages(): array
    {
        return [
            'index' => Pages\ListEvents::route('/'),
            'create' => Pages\CreateEvent::route('/create'),
            'edit' => Pages\EditEvent::route('/{record}/edit'),
            'view' => Pages\EventAttendees::route('/{record}')
        ];

I have tried and tried various queries but can't for the life of me get a list of users to show in the blade view. If I query the event and user who created it I get this back in the view so that part is working.

I would be grateful if you could assist a 70 year old trying to keep dementia at bay!!

0

There are 0 answers