The main logic here is split into two parts - one for Filament and another for a custom user-facing design:
excerpt
field and an actionHere's what that looks like in our code:
app/Filament/Resources/UserResource.php
public static function form(Form $form): Form{ return $form ->schema([ Forms\Components\TextInput::make('name') ->required() ->maxLength(255), Forms\Components\TextInput::make('email') ->email() ->required() ->maxLength(255), Forms\Components\DateTimePicker::make('email_verified_at'), Forms\Components\TextInput::make('password') ->password() ->required() ->maxLength(255), // Instead of normal FileUpload, we use SpatieMediaLibraryFileUpload component Forms\Components\SpatieMediaLibraryFileUpload::make('featured_image') // We want to validate the field in real-time, so we use the `live()` method ->live() // Tells the field that it has to be an image ->image() ->hiddenLabel() // We tell which collection we want to use ->collection('avatar') // Enabling the image editor ->imageEditor() // Defining the image rules ->rules(Rule::dimensions()->maxWidth(600)->maxHeight(800)) // After the image is uploaded, we want to validate it ->afterStateUpdated(function (Forms\Contracts\HasForms $livewire, Forms\Components\SpatieMediaLibraryFileUpload $component) { $livewire->validateOnly($component->getStatePath()); }), ]);}
All of this works because we have installed the spatie/laravel-medialibrary
package and configured our user to use it:
app/Models/User.php
use Filament\Models\Contracts\FilamentUser;use Spatie\MediaLibrary\HasMedia;use Spatie\MediaLibrary\InteractsWithMedia;// ... class User extends Authenticatable implements FilamentUser, HasMedia{ // ... use InteractsWithMedia; // ...}
That's it for our user. Now let's take a look at our Post
...