You are viewing the documentation for an older version of this package. You can check the version you are using with the following command:
composer show spatie/laravel-medialibrary
When adding a file to the medialibrary you can pass an array with custom properties:
$mediaItem = $newsItem
->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');
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 = $newsItem
->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');