如何在laravel中使用jquey AJAX 存储1500多个字段的表单

moiiocjp  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(113)


如何在Laravel 5.8中存储超过1500个字段的表单?

控制器:

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

查看文件:

@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

在我的函数中,我只得到一个1000个字段的数组,但在我的表单中有1514个字段。我该怎么办?

cu6pst1q

cu6pst1q1#

首先,为了克服超时问题,您可以在函数的顶部使用:

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

你也可以在保存数据之前将数据分成块,例如:

$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());
}

希望能有所帮助:)

相关问题