Adding a file to the media library is easy. Just pick one of the starting methods, optionally add some of the middle methods
and finish with a finishing method. All start and middle methods are chainable.
For example:
$yourModel
->addMedia($pathToFile)
->withCustomProperties(['mime-type' => 'image/jpeg'])
->preservingOriginal()
->toMediaCollection();
##Starting methods
##addMedia
public function addMedia($file)
##addMediaFromUrl
This method only accepts URLs that start with http://
or https://
public function addMediaFromUrl(string $url)
##addMediaFromDisk
public function addMediaFromDisk(string $key, string $disk = null): FileAdder
##addMediaFromRequest
public function addMediaFromRequest(string $keyName): FileAdder
##addMultipleMediaFromRequest
public function addMultipleMediaFromRequest(array $keyNames): Collection
Please note the return type of addMultipleMediaFromRequest
is a collection of FileAdder
s. This means you'll have to loop over every FileAdder
to use any of the middle methods. For example:
$fileAdders = $this->model
->addMultipleMediaFromRequest(['file-one', 'file-two'])
->each(function ($fileAdder) {
$fileAdder->toMediaCollection();
});
##addAllMediaFromRequest
public function addAllMediaFromRequest(): Collection
Please note the return type of addAllMediaFromRequest
is a Collection of FileAdder
s. This means you'll have to loop over every FileAdder
to use any of the middle methods. See the addMultipleMediaFromRequest
method above for an example.
##addMediaFromBase64
public function addMediaFromBase64(string $base64data, ...$allowedMimeTypes): FileAdder
##addMediaFromString
public function addMediaFromString(string $string): FileAdder
The file will be named 'text.txt' by default. A specific file name can be set using usingFileName
$model
->addMediaFromString('my string')
->usingFileName('custom-filename.txt')
->toMediaCollection();
##copyMedia
public function copyMedia($file)
##Middle methods
##preserveOriginal
public function preservingOriginal()
##usingName
public function usingName($name)
##setName
This is an alias for usingName
##usingFileName
public function usingFileName($fileName)
##setFileName
This is an alias for usingFileName
##withCustomProperties
public function withCustomProperties(array $customProperties)
##Finishing methods
##toMediaCollection
public function toMediaCollection($collectionName = 'default', $diskName = '')
##toMediaCollectionOnCloudDisk
This function does almost the same as toMediaCollection
. It'll store all media on the disk configured in the cloud
key of config/filesystems.php
public function toMediaCollectionOnCloudDisk(string $collectionName = 'default')