2

I am uploading multiple files using ajax and laravel. I storing files names in MySQL database. In the controller, I am checking whether I am getting an array or not. Only else block is working. When I submit data it is not passing an array. I don't know why. Please check my code where I am wrong.

Thank you

 <form  enctype="multipart/form-data">
      {{ csrf_field() }}
      <div class="form-group">

      <div class="row text-center">
      <div class="col-lg-3">
       Select Event
      </div>
      <div class="col-lg-6">
             <select name="eventname"  class="form-control">
                 <option disabled selected>Select</option>
                 @foreach($event as $eve)
                 <option value="{{$eve->id}}">{{$eve->title}}</option>
                 @endforeach
             </select>
      </div>

      </div>
      </div>

      <div class="form-group">

        <div class="row text-center">
        <div class="col-lg-3">
         Upload A file
        </div>
        <div class="col-lg-6">
               <input type="file" id="eventgallery" class="form-control" name="eventgallery[]" multiple>
        </div>

        </div>

      </div>
      <div class="form-group">
        <div id="images">

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

ajax code

$('#eventgallery').on('change',function(){
    var image = '';
    $.ajax({
      url : '{!! url('uploadimage') !!}',
      date: new FormData(this.form),
      method:'POST',
      dataType: 'json',
      contentType:false,
      cache:false,
      processData:false,
      success:function(data)
      {
          console.log(data);
      }

    });

});

controller

 public function store(Request $request)
    {
        $name = $request->file('eventgallery');

        if (is_array($name) || is_object($name))
        {
            foreach($name as $file)
            {
                $extension = $file->getClientOriginalExtension();
                $filename = asset('uploads/eventgallery'.$request->eventname.rand(11111,9991).'.'.$extension);
                $file->move("uploads/eventgallery",$filename);

                $event = EventGallery::create([
                    'event_name' => $request->eventname,
                    'eventimage_url' => $filename
                ]);
            }
            $eve = Event::find($request->eventname);
            $eventgallery = $eve->galleryies;
            return response()->json($eventgallery);
        }
        else
        {
            return response()->json('it is not a array');

        }



   }
2
  • What error you are getting in console? Commented Dec 28, 2019 at 5:26
  • 1
    if I remove If condition it is showing invalid argument supplied to foreach or it is not an array else block Commented Dec 28, 2019 at 5:31

1 Answer 1

2

Problem is you are sending the post variables as date. It should be data.

$('#eventgallery').on('change',function(){
    var image = '';
    $.ajax({
      url : '{!! url('uploadimage') !!}',
      data: new FormData(this.form),
      method:'POST',
      dataType: 'json',
      contentType:false,
      cache:false,
      processData:false,
      success:function(data)
      {
          console.log(data);
      }

    });

});
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much. A silly mistake and trying from last 2 hours.why it was not showing any error?
I tried the same code. You should check the console for errors. It showed me 500 error code with line number in console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.