MattB's avatar
Level 2

Multi image upload not working

I'm trying to get a form working with multi-image uploads too. When I try the upload, all I get is this error and I can't see where it's gone wrong:

Invalid argument supplied for foreach()

I don't know if I've done the form correctly for the upload part but here is the section for the image upload:

 {!! Form::open(['method' =>'POST', 'action'=> 'AdminGamesController@store', 'files'=>true, 'enctype'=>'multipart/form-data']) !!}

<div class="form-group {{$errors->has('image') ? 'has-error' : ''}}">
  {!! Form::label('image', 'Image 1:') !!}
  {!! Form::file('image', null, ['class'=>'form-control'])!!}
  @if($errors->has('image'))
    {{$errors->first('image')}}
  @endif
</div>
<div class="form-group {{$errors->has('image') ? 'has-error' : ''}}">
    {!! Form::label('image', 'Image 2:') !!}
    {!! Form::file('image', null, ['class'=>'form-control'])!!}
    @if($errors->has('image'))
        {{$errors->first('image')}}
    @endif
</div>
<div class="form-group {{$errors->has('image') ? 'has-error' : ''}}">
    {!! Form::label('image', 'Image 3:') !!}
    {!! Form::file('image', null, ['class'=>'form-control'])!!}
    @if($errors->has('image'))
        {{$errors->first('image')}}
    @endif
</div>
<div class="form-group {{$errors->has('image') ? 'has-error' : ''}}">
    {!! Form::label('image', 'Image 4:') !!}
    {!! Form::file('image', null, ['class'=>'form-control'])!!}
    @if($errors->has('image'))
        {{$errors->first('image')}}
    @endif
</div>
<div class="form-group">
        {!! Form::submit('Add Game', ['class'=>'btn btn-primary']) !!}
      </div>
      {!! Form::close() !!}

And here is the method from the controller:

    public function store(GamesRequest $request)
    {
          $games = new Game();
          $games->image = 'blank';
          $games->title = $request->title;
          $games->score = $request->score;
          $games->description = $request->description;
          $games->price = $request->price;
          $games->category_id = $request->category_id;
          $games->promote = 0;
          $games->manual = $request->manual;
          $games->box = $request->box;
          $games->realphoto = $request->realphoto;
          $games->youtube = $request->youtube;
          $games->sold = 0;
          $games->save();

        // Handle multiple file upload
        $images = $request->file('image');
        foreach($images as $key => $image) {
            $name = $images->getClientOriginalName();
            if ($images->move('images/games/', $name)) {
                // store image to database.
                $image = new Photo();
                $image->photo_id = $request->id;
                $image->image = '/images/games/' . $name;
                $image->save();
            }
        }
          return redirect()->route('admin.games.index');
    }
0 likes
4 replies
Sergiu17's avatar

{!! Form::file('image[]', null, ['class'=>'form-control'])!!}

add this square brackets. and also, you always could do dd($request->file('image')) before passing it to foreach loop

MattB's avatar
Level 2

Ok that got it but now its saying the photo Id column is getting no value from this:

$image->photo_id = $request->id;

How would I get the Id that the request generates for the games table and put it in the photo_id field for each photo in the loop?

Sergiu17's avatar
Sergiu17
Best Answer
Level 60

@mattb maybe $game->id, instead of $request->id ?

$image->photo_id = $game->id;
Sergiu17's avatar

@mattb also, I suggest you to rename photo_id column into game_id in photos table.

Please or to participate in this conversation.