YouTubevideo· Jul 18, 2024· 8:43· 3,200 views on YouTube

Your Livewire Upload Fields Deserve to Look Better

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

22 moments
  1. If you've ever had to implement file storage in a web app, it gets a little bit difficult. It's not the easiest thing in the world, and even with the helpers and facades that Laravel provides, and things like validation, there's still a lot that you have to do on the front end to say, "Hey, I want to accept a file." And if I want to make it look any prettier than the standard HTML file input, well, there's a lot of things that you have to consider. This is a pretty standard file upload component that I whipped together where you have a little bit of flashier CSS, like on this hover.

  2. This is just grabbing from a bunch of different places to copy Tailwind CSS for file upload fields. I could upload a fake resume, and then I see the title of that resume here. Saving it just saves it to the storage facade in the public directory within my Laravel application.

  3. So, if you have a pretty basic setup, and this is all you're looking for when it comes to uploads of files for your users, this might be great. You can get away with like the standard HTML file input and a standard button that you could use. That's pretty much all this is, just with some CSS on top of it. But, if you wanted things like, okay, the progress indicator of a file. Maybe you're uploading large files. Maybe you want to upload multiple files. Well, that gets a little bit trickier.

  4. You can still do it with plain HTML and CSS, but if you've ever had to start to implement and think about the things of like, okay, now this button has to be disabled when I you know, when I upload one or two or three as while while it's being uploaded, it has to be disabled. Or if I want to handle multiple file uploads, then I have to be able to remove others. You know, drag and drop comes out of the box with most HTML elements for the file upload type, but it's not the prettiest of all things.

  5. So, how can we make this better? Well, I saw just recently the great folks at Spatie created this Filepond Livewire component, where it's pretty much just a drag and drop component that you can of course style and customize, but it gives you things such as, let's take a look, such as multiple files, such as being able to say that this is a required input, or having it be disabled when it's when there's certain parameters that are set, and of course specific placeholders as well.

  6. But, usually, if you are just trying to say, "Hey, I only have this one spot on my application that I need a file upload." Maybe if you're not already using something like Filament for uploads, then you don't necessarily need to grab that whole package just for uploads.

  7. This is kind of a great one-off package that we can go ahead and implement in our application right now. So, here in the code, we're doing some basic validation just to say that it's a file that's being uploaded for this resume, and the save, again, some pretty standard stuff. Where we're saying we're going to store this resume and grabbing that path, so that we can actually create a new model instance, a new record of this resume.

  8. And then down here, this is where most of this is just CSS that we're kind of obfuscating the input for file uploads.

  9. But, we're linking that resume wire model, that resume wire variable here, and then just grabbing any kind of errors or any previously uploaded resume. So, again, all pretty standard stuff. But, if we could eliminate all of this to be able to say, because right now we're I'm kind of just using, you know, pure Livewire with Laravel to say if there's a resume uploaded, you can see here that this resume file input is set to live.

  10. So, when we do attach a resume or upload a resume to this input a element here, then we can hide the actual upload portion and just show this.

  11. And that's well and good. That's that's great. But again, can we just make this a lot cleaner and just replicable across the entire application instead of having to, you know, either copy this or export it into its own component and then you have to deal with all those extra variables and stuff like that. This video is mostly just to show the Spatie package off. So, let's go ahead and do that. Okay, let's go ahead and install it.

  12. Perfect. Now, we'll use these scripts to the layout or view where you want to use FilePond. In this case, the FilePond Spatie component that we're using here.

  13. So, why don't we add this um to I have this welcome view that we're using here. So, why don't we go ahead and add it there. And then, we have to add this with FilePond trait to our component. So, why don't we go ahead and do that. So, this is going to be taking the place that the with file uploads trait that Laravel Livewire provides. And again, Laravel Livewire does make this a lot easier than if you were having to manually do all of this connection yourself.

  14. For example, you have the ability to do um multiple files or if you want to even do any kind of, let's see, uploading directly to S3 or any kind of loading indicators or progress indicators, you can do that because Alpine and Livewire are so interconnected. There's great documentation that I'll link in the description that you can take check take a look if you want to do this manually.

  15. But because we're taking a look at this package, I just want to show how easy something like this could be. Again, if you are only needing this for maybe one component or two components across your entire application. So, we're going to say use with FilePond. We'll just grab the location here.

  16. And then, we can add the component to our view, and model it up to the specific attribute. In this case, the resume that we're doing. So, why don't we go ahead and grab that x-file-pond upload. So, I'm just going to remove all of this. And I'm going to say wire model, and we're going to use this with the resume. Okay, and let's take a look what that does.

  17. Okay, because of my styles, looks like it's um s- not the greatest looking. So, after some styling adjustments, that looks a lot better. And now, I can have this ability to have this new little drag and drop. We have things like uploading percentage working correctly.

  18. And let me refresh again. We have things like uploading percentage working all correctly. And then we have the actual title of the file. We have everything like, you know, tap to undo, and we can like redo it and re- upload it, and everything works exactly same as it should work. Yep, there we go. Looks like it doesn't clear. If we want to reset resume, let's see.

  19. Okay, so I'll probably have to figure this out after the video. And there might probably might be a bug, or maybe just how I'm setting it up, where it doesn't look like it wants to reset the input of that component without reloading the entire page. So, that might be something that you or I can dive into after this video. But, the great thing about this component package by Spatie is it just uses filepond under the hood. So, if you're not familiar with filepond, there's a bunch of different instances and properties that filepond accepts.

  20. And it's That's that whole JavaScript kind of element that we saw, where has that drag and drop. It has that progress elements as well. And additionally, you can kind of just publish those views and adjust them as you wish if you wanted to. This is the, you know, Alpine JS vendor file. So, a lot of Alpine, a lot of blade here as well just to kind of accept those props.

  21. Again, this is just the published view for this package. So, the great thing is because it's just on top of FilePond and is just meant to work specifically with your Livewire components, it just works. You don't have to worry about additional options like that. So,

  22. again, I might be able to check out and see exactly why that little piece of reset isn't working like I expect, but give this a try if you're looking for something extremely simple to get file uploads up and running in your application if you didn't already want to pull in a greater package like Filament for example to do forms and uploads like that. The link to this package will be in the creation and until I see you next time, keep creating.

just you here