Laravel-medialibrary: Uploading and Storing Multiple Files from Request Form in Laravel 5.6

Created on 16 Apr 2018  路  1Comment  路  Source: spatie/laravel-medialibrary

It's been a long day, so excuse me asking this seemingly very basic question. Would you happen to have an example of how to upload multiple images and adding them to the media library using Laravel 5.6 and Media Library 7.0.0?

Here's what I have, but it's giving me this error:
The file "/tmp/phpWXeXRw" does not exist

So, I'm obviously missing a step.

Model

use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\HasMedia;

class Product extends Model implements HasMedia
{
    use HasMediaTrait;
    ...
}

View

{!! Form::model($product, ['method' => 'PATCH', 'route' => ['products.update', $product->name], 'id' => 'product-form', 'files' => true]) !!}
    {!! Form::label('images', 'Images:', null) !!}
    {!! Form::file('images[]', ['multiple']) !!}
{!! Form::close() !!}

Controller

    public function update(Product $product, ProductRequest $request)
    {
        if($request->hasfile('images')) {
            foreach($request->file('images') as $image) {
                if($image->isValid()) {
                    $product->addMedia($image)->toMediaCollection('product-images');
                }
            }
        }
    ...

Am I forgetting something? Thanks in advance for any help with this.

Other issues that seem relevant:
https://github.com/spatie/laravel-medialibrary/issues/227

Most helpful comment

Okay, had a good nights rest and came back to it. I noticed it actually is saving to the DB, but it's throwing that same file not found error. I decided to install v7.1.6 which includes some improvements around addMultipleMediaFromRequest() (https://github.com/spatie/laravel-medialibrary/issues/629). This worked and now I'm moving forward again.

composer require spatie/laravel-medialibrary

composer require spatie/laravel-medialibrary:^7.1.6

        $fileAdders = $product
            ->addMultipleMediaFromRequest(['images'])
            ->each(function ($fileAdder) {
                $fileAdder->toMediaCollection('product-images');
            });

>All comments

Okay, had a good nights rest and came back to it. I noticed it actually is saving to the DB, but it's throwing that same file not found error. I decided to install v7.1.6 which includes some improvements around addMultipleMediaFromRequest() (https://github.com/spatie/laravel-medialibrary/issues/629). This worked and now I'm moving forward again.

composer require spatie/laravel-medialibrary

composer require spatie/laravel-medialibrary:^7.1.6

        $fileAdders = $product
            ->addMultipleMediaFromRequest(['images'])
            ->each(function ($fileAdder) {
                $fileAdder->toMediaCollection('product-images');
            });
Was this page helpful?
0 / 5 - 0 ratings