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

El Klo's avatar
Level 11

FileUpload to attachments()

I have a HeaderAction on a Filament resource. This Action contains a form and an actual action. The form accepts a FileUpload as follows:

FileUpload::make('attachments')
		->label('Bijlagen')
		->preserveFilenames()
		->directory('/email/attachments')
		->multiple()

The action on the form should grab the FileUpload and use it as an attachment in a Mailable:

The action:

->action(function (array $data) {
			Mail::to($data['recipient'])->queue(new GenericMail(subject: $data['subject'], body: $data['body'], attachments: $data['attachments']));
				}),

Attachments function inside the Mailable:

public function attachments(): array {
        $attachments = [];

        if (!empty($this->attachments) && is_array($this->attachments)) {
            // Loop through each file path and create an Attachment instance
            foreach ($this->attachments as $filePath) {
                if (is_string($filePath) && !empty($filePath)) {
                    $attachments[] = Attachment::fromStorage($filePath);
                }
            }
        }

        return $attachments;
    }

When i work through my queue, this fails with the following error:

Cannot access offset of type string on string

I know i'm doing some bad array conversion but i can't seem to figure out what and where.

0 likes
1 reply
El Klo's avatar
El Klo
OP
Best Answer
Level 11

I managed to get it to work by replacing the $attachment variable with another. I'm guessing $attachment is reserved and needs a certain set of data. here's the code for anyone else who might be struggling with this:

public array $filePaths;

public function attachments(): array {
        $filePaths = [];
        if (!empty($this->filePaths) && is_array($this->filePaths)) {
            // Loop through each file path and create an Attachment instance
            foreach ($this->filePaths as $filePath) {
                if (is_string($filePath) && !empty($filePath)) {
                    $filePaths[] = Attachment::fromPath(storage_path('app/public/' . $filePath));
                }
            }
        }
        return $filePaths;
    }

Please or to participate in this conversation.