- Basics
- Disk
- Directory
- Allowed Extensions
- Multiple Upload
- File Removal
- Disable Download
- Original File Name
- Custom File Name
- Element Names
- Element Attributes
- Helper Methods
Basics
Before using, ensure that a symbolic link is set up for the storage directory.
php artisan storage:link
The File field is used for file uploads and includes all basic methods.
use MoonShine\UI\Fields\File; File::make('File')
<x-moonshine::form.wrapper label="File"> <x-moonshine::form.file name="file" /></x-moonshine::form.wrapper>
To correctly generate the file URL, you must set the environment variable APP_URL
to match your application's URL.
Disk
The disk()
method allows you to change the filesystems disk.
disk(string $disk)
File::make('File') ->disk('public')
By default, the disk public
is used. You can change it in the configuration file.
When using the local
driver, the returned url
is not a valid URL. For this reason, we recommend always naming your files in a way that will create valid URLs.
Directory
By default, files will be saved in the root directory of the disk. The dir()
method allows you to specify a directory for saving files relative to the root directory.
dir(string $dir)
File::make('File') ->dir('docs')
Allowed Extensions
Using the allowedExtensions()
method, you can specify which files are available for upload.
allowedExtensions(array $allowedExtensions)
File::make('File') ->allowedExtensions(['pdf', 'doc', 'txt'])
Multiple Upload
To upload multiple files, use the multiple()
method.
multiple(Closure|bool|null $condition = null)
File::make('File') ->multiple()
[!ERROR] The field in the database must be of type text or json. You also need to add type casting for the eloquent model - json, or array, or collection.
File Removal
To enable file removal, you need to use the removable()
method.
removable( Closure|bool|null $condition = null, array $attributes = [])
-
$condition
- the condition for executing the method, -
$attributes
- additional attributes for the button.
File::make('File') ->removable()
The delete button will be available in the editing mode of the field.
When deleting a resource, the files are also deleted.
[!IMPORTANT] When mass deleting resource items, files are not deleted. You need to implement file deletion for mass resource item deletion yourself.
Attributes
File::make('File') ->removable( attributes: ['@click.prevent' => '$event.target.closest(`.x-removeable`).remove()'] )
disableDeleteFiles()
The disableDeleteFiles()
method allows you to delete only the database record but not the actual file.
disableDeleteFiles()
File::make('File') ->removable() ->disableDeleteFiles()
enableDeleteDir()
The enableDeleteDir()
method deletes the directory specified in the dir()
method if it is empty.
enableDeleteDir()
File::make('File') ->dir('docs') ->removable() ->enableDeleteDir()
Disable Download
The disableDownload()
method allows you to exclude the ability to download the file.
disableDownload(Closure|bool|null $condition = null)
File::make('File', 'file') ->disableDownload()
Original File Name
When uploading, a file name is generated by default. The keepOriginalFileName()
method allows you to keep the original file name.
keepOriginalFileName()
File::make('File') ->keepOriginalFileName()
Custom File Name
The customName()
method allows you to save a file with a custom name.
customName(Closure $name)
use Illuminate\Http\UploadedFile;use Illuminate\Support\Str; File::make('File', 'file') ->customName(fn(UploadedFile $file, Field $field) => Str::random(10) . '.' . $file->extension())
Element Names
The names()
method allows you to change the displayed name without altering the file name.
names(Closure $closure)
-
$closure
- a closure that takes the file name and the file index as parameters.
File::make('File', 'file') ->names(fn(string $filename, int $index = 0) => 'File ' . $index + 1)
Element Attributes
The itemAttributes()
method allows you to add additional attributes to the elements.
itemAttributes(Closure $closure)
-
$closure
- a closure that takes the file name and the file index as parameters.
File::make('File', 'file') ->itemAttributes(fn(string $filename, int $index = 0) => [ 'style' => 'width: 250px; height: 250px;' ])
Helper Methods
getRemainingValues()
The getRemainingValues()
method allows you to get the values that remain in the form, taking into account deletions.
getRemainingValues()
removeExcludedFiles()
The removeExcludedFiles()
method allows you to physically delete files during the process.
removeExcludedFiles()