Adding custom properties

When adding a file to the medialibrary you can pass an array with custom properties:

$mediaItem = $newsItem
    ->addMedia($pathToFile)
    ->withCustomProperties(['primaryColor' => 'red'])
    ->toMediaLibrary();

There are some convenience methods to work with custom properties:

$mediaItem->hasCustomProperty('primaryColor'); // returns true
$mediaItem->getCustomProperty('primaryColor'); // returns 'red'

$mediaItem->hasCustomProperty('does not exists'); // returns false
$mediaItem->getCustomProperty('does not exists'); // returns null

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']
    ])
    ->toMediaLibrary();

$mediaItem->hasCustomProperty('group.primaryColor'); // returns true
$mediaItem->getCustomProperty('group.primaryColor'); // returns 'image/jpeg'

$mediaItem->hasCustomProperty('nested.does-not-exist'); // returns false
$mediaItem->getCustomProperty('nested.does-not-exist'); // returns null