Im trying to upload more than a single file so I try to modify the code a little bit. But everytime I'm adding foreach($request->file('image') as $file). I always get an error about having undefined variable. I attached my controller code below, Any idea how can I fix this and make the multiple file upload?
if($request->hasFile('image'))
{
foreach($request->file('image') as $file)
{
$filenameWithExt =$request->file('image')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename. '_'.time().'.'.$extension;
$path = $request->file('image')->storeAs('public/images', $fileNameToStore);
}
}
$post = new Post;
$post->image = $fileNameToStore;
$post->save();
return redirect('/dashboard')->with('success', 'Post Created!');
Here is the create.blade.php
@extends('layouts.app')
@section('content')
<h1>Upload Images</h1>
{!! Form::open(['action' => 'PostsController@store', 'method'=> 'POST','enctype'=>'multipart/form-data'])!!}
<div class="form-group">
{{Form::file('image',['multiple'=>'true', 'files'=>'true'])}}
</div>
{{Form::submit('Submit',['class' => 'btn btn-success'])}}
{!!Form::close()!!}