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

mstdmstd's avatar

How from table(Table $table): Table method action upload generated file?

In laravel 10 / filamentphp 3 app in listing I have custom action, which make some updates on selected rows and I need to generate doc file in table(Table $table): Table method :

public static function table(Table $table): Table
{
    return $table
        ->columns([
            TextColumn::make('id')->sortable(),
            ...
        ])
        ->actions([
            ActionGroup::make([

                Action::make('Move To Department')
                    ->form([
                        TextInput::make('FromDepartmentId')
                            ->afterStateHydrated(function (TextInput $component, $state) {
                                $employeeModel = $component->getModelInstance();
                                if(!empty($employeeModel->department)) {
                                    $component->state($employeeModel->department->name);
                                }
                            })->disabled()
                            ->label('From department'),

                        Select::make('move_to_department_id')->label('Move to department')
                            ->prefixIcon(DepartmentHelper::getIcon())
                            ->default(fn(Employee $employee): int => $employee->department_id)
                            ->preload()
                            ->options(Department::query()->getByBranchId(Filament::getTenant()->getAttribute('id'))->pluck('name',
                                'id'))
                            ->required(),
                        TextInput::make('move_to_notes')
                            ->required()
                            ->maxLength(500),
                        Checkbox::make('generating_word_document')->label('Generating word document')->default(true)->inline()

                    ])
                    ->action(function (Employee $employee, array $data, array $arguments): void {
                            throw_if( $employee->department_id === $data['move_to_department_id'],
                                EmployeeInvalidOperation::class,
                                'Can not move employee to the same department'
                            );

                        if ( ! empty($data['move_to_notes']) and ! empty($data['move_to_department_id'])) {
                            try {
                                Employee::whereId($employee->id)->update([
                                    'department_id' => $data['move_to_department_id'],
                                    'notes' => $employee->notes . '<br> >>>>>> ' . $data['move_to_notes'] . ' on ' .
                                               DateConv::getFormattedDateTime(Carbon::now(config('app.timezone')),
                                                   DatetimeOutputFormat::AS_TEXT),
                                    'updated_at' => Carbon::now(config('app.timezone'))
                                ]);
                                Notification::make()
                                    ->title("Employee's card !")
                                    ->body("Employee was moved to selected department !")
                                    ->success()
                                    ->send();
                                \Log::info(varDump($data, ' -1 $data::'));
                                if($data['generating_word_document']) { // Need to generate doc file

                                    $employeeMovedToDepartmentDocGenerated = new EmployeeMovedToDepartmentDocGenerated;
                                    $employeeMovedToDepartmentDocGenerated->setEmployeeMovement($employee, $data['move_to_department_id']);
                                    $employeeMovedToDepartmentDocGenerated->generate();

                                    $ret = $employeeMovedToDepartmentDocGenerated->download();
                                    \Log::info(varDump($ret, ' -1 employeeMovedToDepartmentDocGenerated $ret::'));
                                    self::downloadDocFile($employee, $ret);
                                }
                            } catch (Exception $e) {
                                \Log::info($e->getMessage());
                            }
                        }
                    })
                    ->slideOver()
            ])
        ]);
}

public static function downloadDocFile($employee, $ret)/*: void */
{
    \Log::info(varDump($ret, ' -1 downloadDocFile $ret::'));
    return $ret;
}

Checking what returned by download of EmployeeMovedToDepartmentDocGenerated class I see in log's dump :

[2024-06-17 16:59:22] local.INFO:  (Object of Symfony\Component\HttpFoundation\BinaryFileResponse) : -1 downloadDocFile $ret:: : Array
(
    [headers] => Symfony\Component\HttpFoundation\ResponseHeaderBag Object
        (
            [headers:protected] => Array
                (
                    [cache-control] => Array
                        (
                            [0] => public
                        )

                    [date] => Array
                        (
                            [0] => Mon, 17 Jun 2024 13:59:22 GMT
                        )

                    [last-modified] => Array
                        (
                            [0] => Mon, 17 Jun 2024 13:59:22 GMT
                        )

                    [content-disposition] => Array
                        (
                            [0] => attachment; filename=employees-of-the-app.docx
                        )

                )

            [cacheControl:protected] => Array
                (
                    [public] => 1
                )

            [computedCacheControl:protected] => Array
                (
                    [public] => 1
                )

            [cookies:protected] => Array
                (
                )

            [headerNames:protected] => Array
                (
                    [cache-control] => Cache-Control
                    [date] => Date
                    [last-modified] => Last-Modified
                    [content-disposition] => Content-Disposition
                )

        )

    [ * content] =>
    [ * version] => 1.0
    [ * statusCode] => 200
    [ * statusText] => OK
    [ * charset] =>
    [ * file] => Symfony\Component\HttpFoundation\File\File Object
        (
            [pathName:SplFileInfo:private] => /mnt/_work_sdb8/wwwroot/lar/quizzes/storage/employees-of-the-app.docx
            [fileName:SplFileInfo:private] => employees-of-the-app.docx
        )

    [ * offset] => 0
    [ * maxlen] => -1
    [ * deleteFileAfterSend] =>
    [ * chunkSize] => 16384
)

class EmployeeMovedToDepartmentDocGenerated has method :

public function download(): \Symfony\Component\HttpFoundation\BinaryFileResponse
{
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($this->phpWord, $this->outputFileFormat);
    $filename = 'employees-of-the-app.docx';
    $objWriter->save(storage_path($filename));

    return response()->download(storage_path($filename));
}

In laravel control action returns response()->download(, but how can I do similar in table(Table $table): Table method ?

0 likes
0 replies

Please or to participate in this conversation.