how to store form with 1500 plus fields using jquey ajax in laravel

49 Views Asked by At

screen shot of previewHow can I store a form with more than 1500 fields in Laravel 5.8?

Controller:

public function store(Request $request)
{
     dd($request->all());
}

View file:

@extends('main')
@section('content')

<form class="add_form" id="add_form" enctype="multipart/form-data">
@csrf
@method('PUT') 
<input type="text" name="text_1" >
.
.
.
.
to 1514 parameters
<button type="button" id="submit" >Submit</button>
</form>

@endsection
@push('scripts')

<script type="text/javascript">
  $(document).ready(function(){
    $("#submit").on("click",function(e){
      e.preventDefault();
      if($("#add_form").valid()){
        $.ajax({
          type:"POST",
          url:"{{ route('froms.store') }}",
          data: new FormData($('#add_form')[0]),
          processData: false,
          contentType:false,

          success: function(data){
            if(data.status==='success'){
                  location.reload();
            }else if(data.status==='error'){
                  location.reload();
            }
          }
        });
      }else{
        e.preventDefault();
      }
    });
  });
</script>

@endsection

In my function, I'm only getting an array of 1000 fields, but in my form there are 1514 fields. How can I handle this?

1

There are 1 best solutions below

0
Hamza Dastgir On

First of all to overcome Time Out problem you can use at top of your function :

ini_set('max_execution_time', 120 ) ; // time in seconds

Also you can divide your data into chunks as well before saving data e.g.

$data_to_insert = [];

foreach ($posts as $post) {

    $data = [
        'item_name' => $Item_value,
    ];

    $data_to_insert[] = $data;
}

$data_to_insert = collect($data_to_insert );

// Make a collection to use the chunk method

// it will chunk the dataset in smaller collections containing 500 values each. 

// Play with the value to get best result
$chunks = $data_to_insert->chunk(500);

foreach ($chunks as $chunk)
{
\DB::table('items_details')->insert($chunk->toArray());
}