spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.78k stars 1.08k forks source link

Added when() method to FileAdder #3620

Closed nikrovir closed 5 months ago

nikrovir commented 5 months ago

My pull request adds syntactic sugar like the one used in eloquent builder to allow the when() method to be used.

Example:

$model->addMedia($file)
    ->preservingOriginal(false)
    ->when(in_array($file->getMimeType(), $availableMimeTypes), function (FileAdder $fileAdder) {
        $fileAdder->withCustomProperties([
            'property' => 'value',
        ]);
    })->toMediaCollection();

Instead:

$media = $model->addMedia($file)->preservingOriginal(false);

if (in_array($file->getMimeType(), $availableMimeTypes)) {
    $media->withCustomProperties([
        'property' => 'value',
    ]);
}

$media->toMediaCollection();
freekmurze commented 5 months ago

Could you also add a test for this and add an example to the readme?

freekmurze commented 5 months ago

Closing due to inactivity.