Spatie Media Library
To use Spatie Media Library, bind a file field to a media collection:
use InertiaUI\Forms\Fields\File;
File::make('photos')
->multiple()
->mediaCollection('photos');Edit forms automatically load the existing media as preview items. On submit, the Form::save() method persists the normal model attributes and syncs the configured media collections, without sending encrypted token arrays to the model:
namespace App\Http\Controllers;
use App\Forms\ProductForm;
use App\Models\Product;
use InertiaUI\Forms\Validate;
class ProductController extends Controller
{
public function update(#[Validate] ProductForm $form, Product $product)
{
$form->save($product);
}
}The method treats the submitted files as the collection's complete contents. Any media missing from the field is deleted, and the retained media follows the submitted order. An absent field leaves the collection untouched. Don't share the collection with another field or feature.
Form::save() syncs the temporary upload tokens of the default, chunked, and direct-to-storage paths. It rejects a Media Library field configured with storeWithForm(), because that path submits native multipart files. Persist those files through Laravel's request file API instead.
You may configure the automatic Media Library writes directly on File and Composer fields. These options stay on the server:
File::make('photos')
->multiple()
->mediaCollection(
collection: 'photos',
disk: 'media',
responsiveImages: true,
);The disk argument selects the destination Laravel filesystem disk. It's separate from File::disk(), which resolves ordinary path-backed bound values for previews.
Syncing Collections Manually
To own the model write and the collection sync yourself, call MediaLibraryUploads::syncCollection() directly. This manual helper reads orderedFormUploads() internally and accepts the same destination disk, responsive image, and per-file configuration options:
use InertiaUI\Forms\FileUploads\MediaLibraryUploads;
// $form is the validated ProductForm and $product is the bound Product model...
$product->update($form->validated(files: false));
MediaLibraryUploads::syncCollection(
request: request(),
model: $product,
field: 'photos',
collection: 'photos',
);The model must be saved before the manual helper runs. Form::save() handles that ordering automatically. Neither workflow makes the model, Media Library, and filesystem writes globally atomic.
Leave disk empty in either API to use the collection's configured disk.
Responsive Images and Media Properties
To generate Spatie responsive images, pass responsiveImages: true. The configure closure customizes each new Media item before it's stored. The closure receives the FileAdder and the submitted upload:
use Illuminate\Support\Str;
use InertiaUI\Forms\FileUploads\MediaLibraryUploads;
use InertiaUI\Forms\FileUploads\SubmittedUpload;
use Spatie\MediaLibrary\MediaCollections\FileAdder;
// $request is the current request and $product is the saved Product model...
MediaLibraryUploads::syncCollection(
request: $request,
model: $product,
field: 'photos',
collection: 'photos',
responsiveImages: true,
configure: function (FileAdder $adder, SubmittedUpload $upload): void {
$file = $upload->getUploadedFile();
$adder
->usingFileName(
Str::ulid().'.'.pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION),
)
->usingName($file->getClientOriginalName())
->withCustomProperties(['source' => 'product-form'])
->onQueue('media');
},
);Responsive image generation follows Spatie's queue configuration. A sync queue driver runs the job immediately, while onQueue() selects its queue. Conversions registered on the model keep using Spatie's queued() and nonQueued() configuration.
Custom Media Models
Restoring and ordering existing media uses the Media model the owner selects, with media-library.media_model as the fallback. A custom Spatie Media model therefore keeps its model hooks throughout the sync.
Temporary Upload Lifecycle
By default, new media keeps the client's original filename. Internally, the helper preserves the staged source until storage, collection cleanup, and ordering have all succeeded. It then deletes the Inertia Forms temporary upload.
A failed sync removes the newly created Media records and leaves the staged upload available for a retry. This rollback doesn't restore existing media that the sync already removed or reordered.