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.