Description
Use FilePond the Laravel way
Before we begin, first install and integrate the FilePond library in your project any way you prefer.
Let's assume we are updating a user avatar and his/her gallery like the form below.
<form action="{{ route('avatar') }}" method="post"> @csrf <!-- For single file upload --> <input type="file" name="avatar" required/> <p class="help-block">{{ $errors->first('avatar') }}</p> <!-- For multiple file uploads --> <input type="file" name="gallery[]" multiple required/> <p class="help-block">{{ $errors->first('gallery.*') }}</p> <button type="submit">Submit</button></form> <script> // Set default FilePond options FilePond.setOptions({ server: { url: "{{ config('filepond.server.url') }}", headers: { 'X-CSRF-TOKEN': "{{ csrf_token() }}", } } }); // Create the FilePond instance FilePond.create(document.querySelector('input[name="avatar"]')); FilePond.create(document.querySelector('input[name="gallery[]"]'), {chunkUploads: true});</script>
Now selecting a file with FilePond input field will upload the file in the temporary directory immediately and append the hidden input in the form. Submit the form to process the uploaded file like below in your controller.
In UserAvatarController.php get and process the submitted file by calling the moveTo() method from the Filepond facade which will return the moved file information as well as delete the file from the temporary storage.
use Illuminate\Http\Request;use Illuminate\Routing\Controller;use Illuminate\Validation\Rule;use RahulHaque\Filepond\Facades\Filepond; class UserAvatarController extends Controller{ /** * Update the avatar for the user. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function update(Request $request) { // Single and multiple file validation $this->validate($request, [ 'avatar' => ['required', Rule::filepond([ 'image', 'max:2000' ]]), 'gallery.*' => ['required', Rule::filepond([ 'image', 'max:2000' ]]) ]); // Set filename $avatarName = 'avatar-' . auth()->id(); // Move the file to permanent storage // Automatic file extension set $fileInfo = Filepond::field($request->avatar) ->moveTo('avatars/' . $avatarName); // dd($fileInfo); // [ // "id" => 1, // "dirname" => "avatars", // "basename" => "avatar-1.png", // "extension" => "png", // "mimetype" => "image/png", // "filename" => "avatar-1", // "location" => "avatars/avatar-1.png", // "url" => "http://localhost/storage/avatars/avatar-1.png", // ]; $galleryName = 'gallery-' . auth()->id(); $fileInfos = Filepond::field($request->gallery) ->moveTo('galleries/' . $galleryName); // dd($fileInfos); // [ // [ // "id" => 1, // "dirname" => "galleries", // "basename" => "gallery-1-1.png", // "extension" => "png", // "mimetype" => "image/png", // "filename" => "gallery-1-1", // "location" => "galleries/gallery-1-1.png", // "url" => "http://localhost/storage/galleries/gallery-1-1.png", // ], // [ // "id" => 2, // "dirname" => "galleries", // "basename" => "gallery-1-2.png", // "extension" => "png", // "mimetype" => "image/png", // "filename" => "gallery-1-2", // "location" => "galleries/gallery-1-2.png", // "url" => "http://localhost/storage/galleries/gallery-1-2.png", // ], // [ // "id" => 3, // "dirname" => "galleries", // "basename" => "gallery-1-3.png", // "extension" => "png", // "mimetype" => "image/png", // "filename" => "gallery-1-3", // "location" => "galleries/gallery-1-3.png", // "url" => "http://localhost/storage/galleries/gallery-1-3.png", // ], // ] }}
This is the quickest way to get started. This package has already implemented all the classes and controllers for you. Next we will discuss about all the nitty gritty stuffs available.