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)
Security note. addMediaFromUrl fetches whatever URL you pass to it from your server. It validates only that the URL starts with http:// or https://, not that the destination is safe to reach. Passing user supplied URLs directly therefore exposes your application to server side request forgery (SSRF), letting an attacker make requests to internal hosts, RFC 1918 ranges, loopback, or cloud metadata endpoints (such as http://169.254.169.254/). Only call addMediaFromUrl with URLs you control, or that you have validated against an allowlist of hosts.
##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 FileAdders. 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 FileAdders. 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();
##addMediaFromStream
public function addMediaFromStream($stream): FileAdder
The file will be named 'text.txt' by default. A specific file name can be set using usingFileName
$model
->addMediaFromStream($stream)
->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')