When adding a file to the media library you can pass an array with custom properties:
$mediaItem = $yourModel
->addMedia($pathToFile)
->withCustomProperties(['primaryColor' => 'red'])
->toMediaCollection();
There are some methods to work with custom properties:
$mediaItem->hasCustomProperty('primaryColor');
$mediaItem->getCustomProperty('primaryColor');
$mediaItem->hasCustomProperty('does not exist');
$mediaItem->getCustomProperty('does not exist');
$mediaItem->setCustomProperty('name', 'value');
$mediaItem->forgetCustomProperty('name');
It is also possible to filter a collection by a custom property using filters. These can either be a simple key value array or a callback to allow for more control:
$filteredCollection = $this->model->getMedia('images', ['primaryColor' => 'red']);
$filteredCollection = $this->model->getMedia('images', function (Media $media) {
return isset($media->custom_properties['primaryColor']);
});
If you are setting or removing custom properties outside the process of adding media then you will need to persist/save these changes:
$mediaItem = Media::find($id);
$mediaItem->setCustomProperty('name', 'value');
$mediaItem->forgetCustomProperty('name');
$mediaItem->save();
You can also specify a default value when retrieving a custom property.
$mediaItem->getCustomProperty('isPublic', false);
If you're dealing with nested custom properties, you can use dot notation.
$mediaItem = $yourModel
->addMedia($pathToFile)
->withCustomProperties([
'group' => ['primaryColor' => 'red']
])
->toMediaCollection();
$mediaItem->hasCustomProperty('group.primaryColor');
$mediaItem->getCustomProperty('group.primaryColor');
$mediaItem->hasCustomProperty('nested.does-not-exist');
$mediaItem->getCustomProperty('nested.does-not-exist');
title: Special custom properties
weight: 2
##ZIP File Folders
The ZIP export stores all media files in the root folder of the ZIP file.
If you want to save media in subfolders, you can do this with the help of the special custom property 'zip_filename_prefix'.
Each media can be assigned to a subfolder.
$mediaItem = Media::find($id);
$mediaItem->setCustomProperty('zip_filename_prefix', 'folder/subfolder/');
$mediaItem->save();
$mediaStream = MediaStream::create('export.zip');
$mediaStream->addMedia($mediaItem);