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

strongs's avatar

Create a printable list of users loaded onto an event

Hi all, I'm a newbie to Filament and also a septuagenarian trying to keep the grey matter working!

The issue I have is in trying to create an EventResource in Filament where users can add themselves to any current event.

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

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

There is also the Pivot table EventUsers.

I have successfully created events where users can load themselves onto but what I am struggling with is creating a list of users that are loaded onto an event.

I asked Larry on this site and followed along with the suggested code but sadly it isn't working.

Where I am now following Lary's suggested solution:

I have a Controller with the following content:

class UserController extends Controller { public $events = []; public function printUsers() { $events = Event::with('eventUser')->first(); /dd($events);/ return view('filament.print.users', compact('events')); } }

A blade view

<x-layouts.app>

And in the EventResource

Tables\Actions\Action::make('print') ->url(route('print.users')) ->label('Print Users') ->icon('heroicon-o-printer'),

The function in the controller seems to be returning the correct data but the issue seems to be when rendering the view. I keep getting an error 'Attempt to read property "title" on bool'.

Is it possible to achieve this within the EventResource rather than sending the user to another page?

Many thanks for your assistance.

0 likes
2 replies
LaryAI's avatar
Level 58

It seems like you're trying to create a printable list of users associated with an event in Filament. From your description, it looks like you're on the right track, but there might be a few issues to address. Let's go through the steps to achieve this within the EventResource.

Firstly, ensure that your relationships are correctly defined in your models. Based on your description, they seem fine, but I'll include them here for clarity:

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

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

Next, let's create a method in the EventResource to generate the list of users for a specific event. We'll use a custom action to trigger this method.

// EventResource

use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables\Actions\Action;

class EventResource extends Resource
{
    // ...

    public static function table(Table $table)
    {
        return $table
            ->columns([
                // Your columns here
            ])
            ->actions([
                Action::make('printUsers')
                    ->label('Print Users')
                    ->action('printUsersList')
                    ->icon('heroicon-o-printer'),
                // Other actions...
            ]);
    }

    public function printUsersList(Record $record)
    {
        $event = Event::with('users')->findOrFail($record->getKey());
        $users = $event->users;

        // You can now return a view or generate a PDF with the list of users
        // For example, using a view:
        return view('filament.pages.print-users', compact('event', 'users'));
    }

    // ...
}

In the above code, we've added a custom action printUsers to the EventResource's table. When this action is triggered, it calls the printUsersList method, which retrieves the event and its associated users. Then, it returns a view with the event and users data.

Now, let's create the view resources/views/filament/pages/print-users.blade.php:

{{-- resources/views/filament/pages/print-users.blade.php --}}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Print Users</title>
    <style>
        /* Your print styles here */
    </style>
</head>
<body>
    <h1>Users for Event: {{ $event->title }}</h1>
    <ul>
        @foreach($users as $user)
            <li>{{ $user->name }}</li>
        @endforeach
    </ul>
    <script>
        window.print();
    </script>
</body>
</html>

This view will display the list of users and automatically trigger the print dialog when loaded.

Please note that you'll need to adjust the code to match your actual column names and data structure. Also, ensure that your routes and controller methods are correctly set up to handle the custom action.

Lastly, the error you're encountering ('Attempt to read property "title" on bool') suggests that you might be trying to access a property on a boolean value, which usually happens when a model is not found. Make sure that your controller methods and views are correctly handling the case when an event or user is not found.

strongs's avatar

Thank you Larry but this returns an error 'Unable to call component method. Public method [printUsersList] not found on component'. The function is in the EventResource below the table method

public function printUsersList(Record $record) { $event = Event::with('users')->findOrFail($record->getKey()); $users = $event->users;

    // You can now return a view or generate a PDF with the list of users
    // For example, using a view:
    return view('filament.pages.print-users', compact('event', 'users'));
}

Almost there me thinks!

Please or to participate in this conversation.