I think you are incrementing your loop wrong
for($i = 0; $i < $c; $i++)
{
// code here
}
Notice the difference between the ++ this is where i think your code is wrong it's on the wrong side of the veriable.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm sorry to be a bother but I'm in this crap all day in a simple thing and I'm failing to see why. I'm trying to make a loop to insert in the database multiple data images. If you want to insert an image is working everything right but if I want to insert two or three it inserts only one. To insert two or three with the same table ID related I need to loop the problem I do a return $ C to count the number of images and it returns me always 1. Ando here back to back and this will always give the same. I have something wrong?
public function InserirPortfolio(Request $request) {
$portfolio = Input::except('_token');
$portfolio['id'] = Input::get('id');
$imgportfolio = Input::all();
$validation = Validator::make($imgportfolio, ImagensPortfolio::$imgportfolio);
$validation = Validator::make($portfolio, Portfolio::$portfolio);
if ($validation->passes()) {
if($user = Portfolio::find($portfolio['id'])) {
$user -> update($portfolio);
Session::flash('editportfolio', 'success');
return Redirect::to('backend/portfolio');
}else{
$portfolio = new Portfolio();
$portfolio->title= Input::get('title');
$portfolio->year= Input::get('year');
$portfolio->description= Input::get('description');
$portfolio->genre= Input::get('genre');
$image= Input::file('image');
$count = count($image);
for ( $i=0; $i< $count; $i++) {
$imageportfolio = new ImagePortfolio();
$file = array_get($imgportfolio,'image');
$destinationPath = 'image/portfolio/';
$extension = $image->getClientOriginalExtension();
$fileName = rand(1111,9999) . '.' . $extension;
$imageportfolio ->image = $fileName;
Image::make($file)->resize(700, 400)->save($destinationPath.$fileName);
}
$portfolio->save();
$imageportfolio ->Portfolio()->associate($portfolio);
$imageportfolio ->save();
}
Session::flash('portfolio', 'success');
return Redirect::to('backend/portfolio');
} else {
return Redirect::to('backend/portfolio/editar/'.$portfolio['id'])->withInput()->withErrors($validation);
}
}
My index
<div class="col-md-3 col-lg-3">
{!! Form::label('image', 'Image', ['class' => 'label_perfil']) !!}
</div>
<div class="col-md-9 col-lg-9">
{!! Form::file('image[]', array('multiple'=>true), ['class' =>'input-file' ]) !!}
</div>
Enter the picture only because the rest is working all right. It may be here
Are you sure you use the array syntax for your file input on your form
for instance:
<input type="file" name="image[]">
Otherwise you would not get more than one file instance in return.
Please or to participate in this conversation.