Shawdow's avatar

Invalid argument supplied for foreach()

Hi

I am trying to upload images file name to the database image filename get stored into the database the problem is

I have validator function in controller validation not working show me error

public function upload(Request $request){
     
        $files = Input::file('images');
        
        $file_count = count($files);
        
        $uploadcount = 0;
        
        foreach ($files as $file) {     
            
            $rules = array('file' => 'required');
           
            $validator = \Validator::make(array('file' => $file), $rules);
            
            if($validator -> passes()){
                
                $destinationPath = 'images';
                $filename =  $file->getClientOriginalName();
                $file->move($destinationPath,$filename);
                $uploadcount ++;
               
                $file->getClientOriginalExtension();
                $entry = new \App\image();
                $entry->mime = $file->getClientMimeType();
                $entry->filename = $filename;
                $entry->save();
                
            }
            
        }
        
    return redirect('/admin/image')->with("success","Uploaded Sucessfully");    

   }
0 likes
19 replies
SarnaMC's avatar

Could you show us an error? Apache logs?

Shawdow's avatar
 (1/1) ErrorException

Invalid argument supplied for foreach()
in AdminController.php (line 115)
at HandleExceptions->handleError(2, 'Invalid argument supplied for foreach()', '/var/www/html/laravel1/app/Http/Controllers/AdminController.php', 115, array('request' => object(Request), 'files' => null, 'file_count' => 0, 'uploadcount' => 0))in AdminController.php (line 115)
at AdminController->upload(object(Request))
at call_user_func_array(array(object(AdminController), 'upload'), array(object(Request)))in Controller.php (line 55)
at Controller->callAction('upload', array(object(Request)))in ControllerDispatcher.php (line 44)
at ControllerDispatcher->dispatch(object(Route), object(AdminController), 'upload')in Route.php (line 203)
at Route->runController()in Route.php (line 160)
at Route->run()

......
tykus's avatar

'files' => null that'll be the problem. What does you view template look like?

Shawdow's avatar

@extends('layouts.ad')

@section('content')
       

@if ($message = Session::get('success'))
<div class="row">
    
    <div class="col-md-12">
        

            <div class="alert alert-success">

                <p>{{ $message }}</p>

            </div>
     </div>
    
</div>

@endif

        <div class="col-sm-10 well-lg">
            <div class="row">    
                <div class="container-fluid">
                    <div class="form-group">
                      <h2>Simple Multiple Upload</h2>
                      {!! Form::open(array('url'=>'/admin/upload','method'=>'POST', 'files'=>true)) !!}
                      {!! Form::file('images[]', array('multiple'=>true, 'id' => 'file')) !!}
                        <p>{!!$errors->first('file')!!}</p>
                        @if(Session::has('error'))
                          <p>{!! Session::get('error') !!}</p>
                        @endif
                      {!! Form::submit('Submit', array('class'=>'btn btn-lg btn-primary col-md-4')) !!}
                      {!! Form::close() !!}
                    </div>
                </div>
            </div>
           </div>
            
@endsection

LaraStorm's avatar

try to create a own request and validate from there

Shawdow's avatar

@LaraStorm If i create validation try to upload correct .jpeg image it shows me still "file required not saving to the database"

 public function upload(Request $request){
       
       $this->validate($request,[
         'file' => 'required|mimes:jpeg,png,jpg,gif,svg|max:2048'
        ]);

        $files = Input::file('images');
        
        $file_count = count($files);
        
        $uploadcount = 0;
        
        foreach ($files as $file) {   
            
            
                $destinationPath = 'images';
                $filename =  $file->getClientOriginalName();
                $file->move($destinationPath,$filename);
                $uploadcount ++;
               
                $file->getClientOriginalExtension();
                $entry = new \App\image();
                $entry->mime = $file->getClientMimeType();
                $entry->filename = $filename;
                $entry->save();
                
            }
            
        
    return redirect('/admin/image')->with("success","Uploaded Sucessfully");    

   } 
LaraStorm's avatar

you just wanna upload multiple images to the database ?

Shawdow's avatar

@LaraStorm I am trying to upload multiple images but still the validation show "The file field required"

tykus's avatar

Your validation is failing because you are checking a file input name rather than images.

On the face of it, your form and controller code looks okay. Can you check the request in the browser's dev tools to see that the form data contains the images?

Shawdow's avatar

@tykus forms data does not contains images it stores only images file name for example demo.jpeg!!

tykus's avatar

Are you doing anything (javascript-related) to modify the FormData before the form submits, or is it plain old HTML?

Shawdow's avatar

@tykus Simple blade file

{!! Form::open(array('url'=>'/admin/upload','method'=>'POST', 'files'=>true)) !!}
                      {!! Form::file('images[]', array('multiple'=>true, 'id' => 'filename')) !!}
                        <p>{!!$errors->first('file')!!}</p>
                        @if(Session::has('error'))
                          <p>{!! Session::get('error') !!}</p>
                        @endif
                      {!! Form::submit('Submit', array('class'=>'btn btn-lg btn-primary col-md-4')) !!}
                      {!! Form::close() !!}


Shawdow's avatar

@tykus

I have checked with displaying the data inside the foreach loop also it is working

tykus's avatar

In the controller; so, the images are being submitted?

Shawdow's avatar

@tykus image filenames are getting submitted it is working the problem is in validation.Suddenly the user clicks on the submit button i am trying show validation to user saying image field required or jpeg image format etc.. that i cannot achieve it.something missing in foreach??

tykus's avatar

Right, so the validation is the problem, you need to allow for an array being received:

$this->validate($request, [
    'images.*' => 'required|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
1 like
Shawdow's avatar

@tykus but still lies same problem

ErrorException thrown with message "Invalid argument supplied for foreach()"

Please or to participate in this conversation.