Components
Media Library
Integrations
Media Picker
In order to select items from your Media Library, the packages provides a custom Filament form field called MediaPicker. This field can be used in any Filament form, both inside or outside a Filament panel.
The field consists of two parts: first, when you have items selected, it will display them in your form. Second, it provides a modal to open when the users wants to select or upload items to the media library.
Using the field
You can include the field like this:
Depending on which driver you are using, the value of the field will be either the id of the MediaLibraryItem (if you are using the medialibrary driver) or the file path (if you are using the filesystem driver).
Selecting multiple files
To select multiple files, you can use the ->multiple() method:
If you are storing the keys as an JSON array on your model, you will need to add an
arrayorcollectioncast to the field in your model.
Minimum or maximum files
You can require a minimum or maximum number of files to be selected using the ->minFiles() and ->maxFiles() methods:
Limiting file types
You can limit the file types the user can select using the ->acceptedFileTypes() method:
Please note that this will only limit the selection of files in the media picker. It will not prevent users from uploading other file types to the media library itself. If you would like to prevent that, please look into defining upload constraints.
If you would like to provide a custom validation message, you can use the ->validationMessages() function:
Modifying the select action
To modify the select action that opens the Media Picker modal, you can use the following methods:
Modal
You can also customize the modal that opens when selecting media.
Scoping to folder
By default, the MediaPicker will open in the root folder. However, in some cases, you might want to only allow your users to select/upload images from and to specific folders. You can do that using the ->scopedFolder() function. This forces a specific folder to allow your users to create and select from (e.g. go down in hierarchy), but not to open up parent folders or go up in the hierarchy.
To scope the Media Picker to a specific folder, you can use the ->scopedFolder() method:
Using a closure:
To use this method with the Storage/disk driver, you can provide a
FileDataobject instead of aMediaLibraryFolderobject:
Default folder
If you don't want to force your users to use a specific folder, but only nudge them, you can also use the ->defaultFolder() method. This method allows you to specify a default folder that the MediaPicker will open in. The difference with ->folder() is that this method will allow your users to open other folders as well, also higher in the hierarchy.
The defaultFolder() functions accepts a MediaLibraryFolder or FileData object as parameter. You can also pass a closure, and use that closure to retrieve the folder dynamically from e.g. the current record.
Using a closure:
Note: please note that the media picker will now open this folder by default. However, users are still able to click to other folders and view them. If you have a need to disable this and only force a specific folder, please use the
->scopedFolder()method (see above).
Individual file info on multiple selection
By default, when a user is allowed to select one file, the modal with open the Media Library including the file-info sidebar at the right. When the user is allowed to select multiple files, the sidebar will not be shown by default as that can be confusing to the user. However, if you still wish to enable this, you can use the ->fileInfoMultipleFileSelection() method:
This will enable both the bulk-selection checkboxes as well as the file-info sidebar when selecting multiple files.
Using a closure:
Selected items
Reordering
You can allow your users to reorder the selected items using the ->reorderable() method:
If a user hovers over an image, the cursor will change into a “move” icon, so that it is clear that the media can be reordered.
Downloading
You can allow your users to download the selected items using the ->downloadable() method:
Actions
You can add additional actions to each selected item using the ->fileActions() method:
This will display the actions together with the download and/or delete action.
Bulk actions
You can allow your users to perform bulk actions on the selected items in your field. This is very handy to let the user batch-process an already selected set of files,
To enable bulk selection & actions, use the ->bulkSelectable() method:
Relationship support
This only applies when using the Media Library Item Driver.
You can integrate the MediaPicker with a BelongsTo or BelongsToMany relationship on your model. This is useful when you want to store the relationship between your model and media library items in a proper database relationship instead of storing IDs directly in a column.
The MediaPicker supports both relationship types:
- BelongsTo: For single media library items
- BelongsToMany: For multiple media library items
To integrate a relationship, follow the steps below:
Create the pivot table (BelongsToMany only)
If you're using a BelongsToMany relationship, you'll need to create a pivot table. Skip this step if you're using a BelongsTo relationship.
Create a migration for your pivot table:
In the migration, define the pivot table structure:
Run the migration:
Define relationship
Next, add the relationship method to your model.
For a BelongsTo relationship (single media library item):
For a BelongsToMany relationship (multiple media library items):
If your pivot table doesn't follow Laravel's naming convention, specify it explicitly:
Using the relationship
Finally, configure the MediaPicker to use the relationship using the ->relationship() method:
For a BelongsTo relationship:
For a BelongsToMany relationship:
When using relationships, the MediaPicker will automatically manage the relationship records when the form is saved. You don't need to manually handle the saving of the relationship data.