File

# Basics

Before use, you must make sure that the storage directory a symbolic link has been set.
php artisan storage:link

The File field is used to upload files and includes all the basic methods.

use MoonShine\Fields\File;
 
//...
 
public function fields(): array
{
return [
File::make('File')
];
}
 
//...

To generate the file URL correctly, you must define the APP_URL environment variable in this way, so that it matches your application's URL.

# Disk

The disk() method allows you to change the filesystems disk.

disk(string $disk)
use MoonShine\Fields\File;
 
//...
 
public function fields(): array
{
return [
File::make('File')
->disk('public')
];
}
 
//...

The default is disk public.
You can change it in the file configurations .

When using the local driver, the url value returned is not a URL. For this reason, we recommend that you always store your files with names 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 the directory to save files relative to the root directory.

dir(string $dir)
use MoonShine\Fields\File;
 
//...
 
public function fields(): array
{
return [
File::make('File')
->dir('docs')
];
}
 
//...

# Valid extensions

Using the allowedExtensions() method you can specify which files will be available for download.

allowedExtensions(array $allowedExtensions)
use MoonShine\Fields\File;
 
//...
 
public function fields(): array
{
return [
File::make('File')
->allowedExtensions(['pdf', 'doc', 'txt'])
];
}
 
//...

# Multiload

To load multiple files, use the multiple() method.

multiple(Closure|bool|null $condition = null)
use MoonShine\Fields\File;
 
//...
 
public function fields(): array
{
return [
//...
File::make('File')
->multiple(),
//...
];
}
 
//...

The field in the database must be of text or json type.
You also need to add a cast for the eloquent model - json, or array, or collection.

# Deleting files

To be able to delete files, you must use the removable() method.

removable(
Closure|bool|null $condition = null,
array $attributes = []
)
  • $condition - condition for executing the method,
  • $attributes - additional button attributes.
use MoonShine\Fields\File;
 
//...
 
public function fields(): array
{
return [
File::make('File')
->removable()
];
}
 
//...
Attributes
use MoonShine\Fields\File;
 
//...
 
public function fields(): array
{
return [
File::make('File')
->removable(
attributes: ['@click.prevent' => '$event.target.closest(`.x-removeable`).remove()']
)
];
}
 
//...
disableDeleteFiles()

The disableDeleteFiles() method will allow you to delete only a record in the database, but do not delete the file itself.

disableDeleteFiles()
use MoonShine\Fields\File;
 
//...
 
public function fields(): array
{
return [
File::make('File')
->removable()
->disableDeleteFiles()
];
}
 
//...
enableDeleteDir()

The enableDeleteDir() method deletes the directory specified in the dir() method if it is empty.

enableDeleteDir()
use MoonShine\Fields\File;
 
//...
 
public function fields(): array
{
return [
File::make('File')
->dir('docs')
->removable()
->enableDeleteDir()
];
}
 
//...

# Ban on downloading

The disableDownload() method allows you to exclude the possibility of downloading a file.

disableDownload(Closure|bool|null $condition = null)
use MoonShine\Fields\File;
 
//...
 
public function fields(): array
{
return [
File::make('File', 'file')
->disableDownload()
];
}
 
//...

# Original file name

When loading, a file name is generated by default. The keepOriginalFileName() method allows you to save the file with the original name.

keepOriginalFileName()
use MoonShine\Fields\File;
 
//...
 
public function fields(): array
{
return [
File::make('File')
->keepOriginalFileName()
];
}
 
//...

# Custom file name

The customName() method allows you to save a file with a custom name.

customName(Closure $name)
use MoonShine\Fields\File;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Str;
 
//...
 
public function fields(): array
{
return [
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 display name without changing the file name.

names(Closure $closure)
  • $closure - the closure takes the file name and file index.
use MoonShine\Fields\File;
 
//...
 
public function fields(): array
{
return [
File::make('File', 'file')
->names(fn(string $filename, int $index = 0) => 'File ' . $index + 1)
];
}
 
//...

# Item attributes

The itemAttributes() method allows you to add additional attributes to elements.

itemAttributes(Closure $closure)
  • $closure - the closure takes the file name and file index.
use MoonShine\Fields\File;
 
//...
 
public function fields(): array
{
return [
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 remained in the form, taking into account the deletion.

getRemainingValues()
removeExcludedFiles()

The removeExcludedFiles() method allows you to physically remove files during the process.

removeExcludedFiles()

Recipe: saving images in the linked table.