Laravel-medialibrary: Silly noob question about adding multiple files

Created on 11 Jul 2017  路  1Comment  路  Source: spatie/laravel-medialibrary

I'e looked around in the API and the documentation but has not found a good example that deals with the scenario where I have 1 file input in my form that accepts multiple files, and I want all of these files to be added to my model.

I have written this mess right here and it works as I expect, however, is there a cleaner or better way to do this?

<?php

collect($model->addMultipleMediaFromRequest(["images"])
    ->first())
    ->each(function ($adder) {
        $adder->toMediaLibrary();
    });

i collect the result from addMultipleMediaFromRequest because it returns an array (with only 1 item...) and need to loop through each fileAdder objects within that item.

The ideal syntax would be something like this:

<?php

$model->addMultipleMediaFromRequest("images")
    ->each(function ($adder) {
        $adder->toMediaLibrary();
    });


// Even better
$model
    ->addMultipleMediaFromRequest("images")
    ->allToMediaLibrary();

Any tips on this would be great, I really like this package, but I think the documentation and DevUX for common issues like this should be easier to use/find and get going quickly.

Most helpful comment

There was a bug regarding adding multiple media, it should be fixed in version 5.12.1.

This syntax should do the trick:

$model->addMultipleMediaFromRequest(["images"])
    ->each(function ($fileAdder) {
        $fileAdder->toMediaCollection();
    });

I think you could use Collection's higher order methods too...

$model->addMultipleMediaFromRequest(["images"])->each->toMediaCollection();

>All comments

There was a bug regarding adding multiple media, it should be fixed in version 5.12.1.

This syntax should do the trick:

$model->addMultipleMediaFromRequest(["images"])
    ->each(function ($fileAdder) {
        $fileAdder->toMediaCollection();
    });

I think you could use Collection's higher order methods too...

$model->addMultipleMediaFromRequest(["images"])->each->toMediaCollection();
Was this page helpful?
0 / 5 - 0 ratings