Now, let's add a file upload field to the Tasks CRUD.
Prepare Back-End: Spatie Media Library
Let's install a well-known package spatie/laravel-medialibrary to handle the file data.
composer require "spatie/laravel-medialibrary"
Then, according to its documentation, we need to publish and run migrations:
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-migrations"php artisan migrate
Finally, we add the appropriate Traits to the Task Model file.
app/Models/Task.php:
use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia; // ... class Task extends Modelclass Task extends Model implements HasMedia//[ tl! ++]{ use HasFactory; use InteractsWithMedia; // ...
Ok, the package installation/configuration is ready. Let's dive into the forms.
File Upload in Create Form: Front-End
This time, let's start with the front end.
For reference, here's the Inertia documentation on File Uploads.
Let's see how our Create.tsx file needs to change.
- We add
mediato theCreateTaskFormanduseForm - We add a
progressindicator from Inertia - We add the
<Input>for the file picker
We start with the TypeScript changes...
Getting error from the resources/js/pages/tasks/create.tsx and the edit.tsx The files both work and I am getting the info on to the web page. When looking over the pages the only thang I can see that is being marked as ban are the <Input id="media" onChange={(e) => setData('media', e.target.files[0])} className="mt-1 block w-full" type="file" /> This is on both pages On the Edit.tsx you mentioned the put problem I am getting this as well.
PS. I am using PhpStorm as my editor.
Wait, so the files are working but you're worried about IDE warnings, only?
You can get rid of the linter error by applying a conditional to make sure
e.target.files[0]is not null first: