Reputation: 47
so this is my sotre controller
$images = $request->file('image');
$file_count = count($images);
$uploadcount = 0;
$daily = new Report();
foreach($images as $image) {
$daily = new Report();
$destination ='img/report';
$filename = $image->getClientOriginalName();
storage::put('img/report/'.$filename,file_get_contents($image->getRealPath()));
$daily->image = $filename;
$uploadcount ++;
}
$daily->author = $request->author;
$daily->desc = $request->desc;
// $daily->created_at = Carbon::now();
$daily->save();
if($uploadcount == $file_count){
return redirect('/daily');
}else{
return redirect('/daily/create');
}
and this is my Blade :
<form action="/daily/create" method="POST" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}">
{{-- date --}}
{!! Form::text('date', old('date', Carbon\Carbon::today()->format('d-m-Y')),['class'=>'form-control date-picker']) !!}
{{-- penulis --}}
<label for="author">Author :</label>
<input name="author" type="text" value="{{ Auth::user()->name }}">
{{-- textarea --}}
<label for="desc">Description</label>
<textarea name="desc" id="" cols="30" rows="10"></textarea>
{{-- input image --}}
<div class="file-field input-field">
<div class="btn">
<span>File</span>
<input type="file" name="image[]" multiple>
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text" placeholder="Upload one or more files">
</div>
</div>
<center>
<input type="submit" class="btn">
</center>
</form>
all images has been add and saved to folder, but i just get the last image name.. i read this post how to save multiple images in table using laravel php many time and still didn't know how to fix it hellppppppp...
Upvotes: 0
Views: 676
Reputation: 4153
The problem here is obvious, you are able only to save 1 data because you are not saving each
new Report()
inside the foreach loop
What you did is you save it after the loop is finish which is the only save data is last new Report()
you created inside the foreach loop
To solve that place it place this code
$daily->author = $request->author;
$daily->desc = $request->desc;
// $daily->created_at = Carbon::now();
$daily->save();
inside foreach loop
$images = $request->file('image');
$file_count = count($images);
$uploadcount = 0;
foreach($images as $image) {
$daily = new Report();
$destination ='img/report';
$filename = $image->getClientOriginalName();
storage::put('img/report/'.$filename,file_get_contents($image->getRealPath()));
$daily->image = $filename;
$uploadcount ++;
$daily->author = $request->author;
$daily->desc = $request->desc;
// $daily->created_at = Carbon::now();
$daily->save();
}
if($uploadcount == $file_count){
return redirect('/daily');
}else{
return redirect('/daily/create');
}
Upvotes: 1