{!! 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
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');
}
Please or to participate in this conversation.