Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

josecerejo's avatar

Multiple images in database

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

0 likes
12 replies
morloderex's avatar

@josecerejo

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.

josecerejo's avatar

Thanks for the reply and correct this error had not noticed but still only insert an image in the database and not those that want

morloderex's avatar
Level 17

@josecerejo

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.

josecerejo's avatar

My form it's like this

<div class="row" style="margin-bottom: 20px;">
    <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>
xsmalbil@icloud.com's avatar

@josecerejo hi there. I just want to add something completely different: Your code loses expressiveness pretty fast when you use variable names like $c. Try giving them names that makes your code easier to read.

1 like
josecerejo's avatar

$c = count I have changed the variable name. Thanks for the tip

vtalbot's avatar

Like @lundz said, your image input should be named image[].

{!! Form::file('image', ['multiple' => true], ['class' => 'input-file', 'name' => 'image[]']) !!}
1 like
jonmolnar's avatar

You're returning the $count - that will break the loop.

josecerejo's avatar

I don't have the return was one example. I've taken the code not to confuse

thomaskim's avatar

You should show your entire code. Just copy/paste. There are several variables in your snippet that don't exist so where are they coming from? It's impossible to help you as is.

jonmolnar's avatar

Sure it's even making it to the for loop? ... sheesh.

I'd die and dump $count in the for loop and make sure its a value greater than 0 first.

If you don't get the value returned, then you know it's not getting hit. If it does and it's 0 then you know you aren't getting an array from Input::file('image'); - or at least one with multiple values.

thomaskim's avatar

Yea, I'd start out one by one. For example, lets just start from the beginning.

$portfolio = Input::except('_token'); 
$portfolio['id'] = Input::get('id');
$imgportfolio = Input::all();
$validation = Validator::make($imgportfolio, ImagensPortfolio::$imgportfolio);
$validation = Validator::make($portfolio, Portfolio::$portfolio); 

How does that validation even work? It doesn't make sense. Lol. The second argument should be an array of rules.

Please or to participate in this conversation.