ziben69's avatar

Laravel | Upload multiple photos

Hello again guys, I have small problem. I don't understand where I am making a mistake.

I have model Photo:

 protected $fillable = [
        'filename',
    ];

StoreController:

    public function store(Request $request)
    {
        $photo = Photo::create($request->all());

        if ($request->has('photos')){
            foreach ($request->photos as $photo) {
                $filename = 'img-'.time().'.'.$photo->getClientOriginalExtension();
                $photo->storeAs('public/photos/',$filename);
                Photo::create([
                    'filename' => $filename
                ]);
            }
        }
        return redirect()->action('PhotoController@plist');
    }

and part of blade view:

<div class="form-row">
       <div class="form-group col-md-6" id="photoInput">
              <input type="file" id="uploadPhoto" onchange="ValidateSize(this)" name="photos[]" multiple aria-describedby="fileHelp"/>
       </div>
</div>

I get a negative result when I try to add images:

General error: 1364 Field 'filename' doesn't have a default value (SQL: insert into `photos` () values ())

As if I did not pass the name from the file selection field. Could someone explain to me where the error is in the function?

Thanks

0 likes
1 reply
Nakov's avatar
Nakov
Best Answer
Level 73

I believe that the error lies here:

$photo = Photo::create($request->all());

The first line of your store function, so remove that line and you should be good to go :)

2 likes

Please or to participate in this conversation.