mikromike's avatar

filament/filament 3.x upload file without save database

Hello

How to upload files without save file(s) info to DB?

When there is filament resource, it always looking for model and db table.

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.companies' doesn't exist

so it there a way use filament 3.x form like this without db table:

return $form
          ->schema([
              FileUpload::make('Prospect Upload')
               ->disk('upload')
               ->directory('prospects/json')
               ->visibility('private')
               ->maxSize(512)
               ->preserveFilenames()
               ->multiple()
               ->storeFileNamesIn('attachment_file_names')
               ->uploadButtonPosition('left')
               ->uploadProgressIndicatorPosition('left')
           ]);

thanks Mika.

0 likes
2 replies
LaryAI's avatar
Level 58

To upload files without saving file information to the database using Filament 3.x, you can follow these steps:

  1. Create a new Filament resource for handling the file upload. Let's assume the resource is named "FileUploadResource". Run the following command to generate the resource:
php artisan filament:resource FileUploadResource
  1. In the generated "FileUploadResource" class, modify the fields method to include the file upload field:
use Filament\Resources\Forms\Components\FileUpload;

public function fields()
{
    return [
        FileUpload::make('Prospect Upload')
            ->disk('upload')
            ->directory('prospects/json')
            ->visibility('private')
            ->maxSize(512)
            ->preserveFilenames()
            ->multiple()
            ->storeFileNamesIn('attachment_file_names')
            ->uploadButtonPosition('left')
            ->uploadProgressIndicatorPosition('left'),
    ];
}
  1. In your controller or wherever you handle the form submission, you can access the uploaded files using the attachment_file_names field:
use App\Filament\Resources\FileUploadResource;

public function store(Request $request)
{
    $validatedData = $request->validate([
        'attachment_file_names' => 'required|array',
        'attachment_file_names.*' => 'file|max:512',
    ]);

    // Handle the uploaded files here

    return redirect()->back()->withSuccess('Files uploaded successfully.');
}

Note that you don't need to save the file information to the database since you mentioned that you don't want to save it. You can handle the uploaded files in the controller or wherever you need to process them.

Remember to update the namespace and use statements according to your application's structure.

I hope this helps! Let me know if you have any further questions.

mikromike's avatar

after added previous code, getting error:

BadMethodCallException Method Filament\Forms\Components\FileUpload::query does not exist.

Please or to participate in this conversation.