Sopamo / laravel-filepond

Laravel backend module for filepond uploads
MIT License
203 stars 56 forks source link

Fatal error #25

Closed raveren closed 4 years ago

raveren commented 4 years ago
php artisan --version
Laravel Framework 6.18.3

While using this repo I am presented with an error:

Symfony\Component\Debug\Exception\FatalThrowableError: Cannot use object of type Illuminate\Http\UploadedFile as array in file <redacted path>\vendor\sopamo\laravel-filepond\src\Http\Controllers\FilepondController.php on line 32

#0 [internal function]: Sopamo\LaravelFilepond\Http\Controllers\FilepondController->upload(Object(Illuminate\Http\Request))

I'm fairly new to laravel, and removing the [0] here: https://github.com/Sopamo/laravel-filepond/blob/master/src/Http/Controllers/FilepondController.php#L32 fixes the issue.

noreason commented 4 years ago

I had this issue also a few minutes ago but managed to solve it.

I've never used FilePond or this package before today, but after looking around a bit I saw that the hidden input fields it creates when you upload a file are generated like so:

<input type="hidden" name="file" value="long-encrypted-value">

Notice the attribute name has a value of "file".

This value will probably be different for you depending on how you configured this package (filepond.php) and initialized FilePond. For example, let's assume you initialized like the documentation:

// Create a multi file upload component
const pond = FilePond.create({
    multiple: true,
    name: 'filepond'
});

And configured config\filepond.php to match:

'input_name' => 'filepond',

Your hidden fields that will be passed to the server will be:

<input type="hidden" name="filepond" value="long-encrypted-value">
<input type="hidden" name="filepond" value="another-long-encrypted-value">
...

Now since multiple uploads are probably more common than not, FilePondController looks like it wants to see an array. This is why it is using the [0] index on the $request.

So after knowing that, it made more sense and the fix was a bit more obvious.

TLDR;

Simply change the "name" you are initializing FilePond to "whatever-you-want[]". Notice the brackets as they are the key.

// Create a multi file upload component
const pond = FilePond.create({
    multiple: true,
    name: 'filepond[]'
});

Make sure the above also matches whatever you have set in config\filepond.php

FilePond will now generate the correct input fields:

<input type="hidden" name="filepond[]" value="long-encrypted-value">
<input type="hidden" name="filepond[]" value="another-long-encrypted-value">
...

So now that it's passing an array of multiple filepond items, everything should work for you without needed to modify the vendor file.

Sopamo commented 4 years ago

This has been fixed in v0.4.