Laravel使用刀片复制所有表单字段,并使用eloquent

时间:2018-03-17 23:13:33

标签: php laravel laravel-5 laravel-5.5 bulkinsert

我想添加另一个表单按钮,它将复制表单的所有字段,但仍然会保留一个提交按钮,并将其中的所有数据传递给控制器​​,以便使用一个查询插入它。

这是打开表单

create.blade.php
@extends('encoder-dashboard.layouts.app')

@section('css')
  <link rel="stylesheet" href="/css/datepicker.css">
@endsection

@section('content')
    <section class="content-header">
        <h1>
            Add New Analysis Request
        </h1>
    </section>
    <div class="content">
        @include('adminlte-templates::common.errors')
        <div class="box box-primary">

            <div class="box-body">
                <div class="row">
                    {!! Form::open(['route' => 'encoder.analysis-request.store']) !!}

                        @include('encoder-dashboard.analysis-request.fields')

                    {!! Form::close() !!}
                </div>
            </div>
        </div>
    </div>
@endsection

@section('scripts')
  <script src="/js/datepicker.js"></script>

  <script>
  $('#datepicker').datepicker({
      autoclose: true
    });
  </script>
@endsection

以下是 fields.blade.php

中列出的字段
<!-- Client Id Field -->
<div class="form-group col-sm-6">
    {!! Form::label('client_id', 'Client Name:') !!}
     {!! Form::select('client_id[]', $client, null, ['class' => 'form-control','required'])!!}
</div>

<!-- Password Field -->
<div class="form-group col-sm-6">
    {!! Form::label('sample_code', 'Sample Code:') !!}
    {!! Form::text('sample_code[]', null, ['class' => 'form-control','required']) !!}
</div>

<!-- Password Field -->
<div class="form-group col-sm-6">
    {!! Form::label('sample_description', 'Description:') !!}
    {!! Form::text('sample_description[]', null, ['class' => 'form-control','required']) !!}
</div>

<!-- Password Field -->
<div class="form-group col-sm-6">
    {!! Form::label('quantity', 'Quantity:') !!}
    {!! Form::number('quantity[]', null, ['class' => 'form-control','required']) !!}
</div>

<!-- Password Field -->
<div class="form-group col-sm-6">
    {!! Form::label('analysis_requested', 'Analysis Request:') !!}
    {!! Form::text('analysis_requested[]', null, ['class' => 'form-control','required']) !!}
</div>

<!-- Password Field -->
<div class="form-group col-sm-6">
    {!! Form::label('special_instruction', 'Special Instruction:') !!}
    {!! Form::text('special_instruction[]', null, ['class' => 'form-control','required']) !!}
</div>

<!-- Submit Field -->
<div class="form-group col-sm-12">
    {!! Form::submit('Save', ['class' => 'btn btn-success', 'onClick' => 'this.form.submit(); this.disabled=true; this.value="Saving…";']) !!}
    <a href="{!! route('encoder.analysis-request.index') !!}" class="btn btn-default">Cancel</a>
</div>

表单上方的所有字段都将被复制并作为数组传递给它?我怎么做也许使用jQuery或简单的vanila JS?

如果我传递这些数据,它将看起来像这些。

array:8 [▼
  "_token" => "xEGtr4h0f4gim0sLvABZivvq36UNaRHWJ08PMWTI"
  "client_id" => array:1 [▼
    0 => "1"
  ]
  "sample_code" => array:1 [▼
    0 => "eqwewqeqewqrwereqwe"
  ]
  "sample_description" => array:1 [▼
    0 => "dasdsadsadsadasd"
  ]
  "quantity" => array:1 [▼
    0 => "232"
  ]
  "analysis_requested" => array:1 [▼
    0 => "dsadsadsadsadsad"
  ]
  "special_instruction" => array:1 [▼
    0 => "dwqewqewqewqewqeq"
  ]
]

这是我在插入一条记录时的查询。

$analysis_request = $this->analysisrequestRepository->create([
            'client_id' => $input['client_id'],
            'sample_code' => $input['sample_code'],
            'sample_description' => $input['sample_description'],
            'quantity' => $input['quantity'],
            'analysis_requested' => $input['analysis_requested'],
            'special_instruction' => $input['special_instruction'],
            'status' => 'for_testing'
        ]);

        $id = $analysis_request->id;

        $request_actors = RequestActors::create([
            'request_id' => $id,
            'encoder_id' => Auth::guard('encoder')->user()->id,
            'microbiologist_id' => null
        ]);

这是我上面的插入一条记录的查询。我还执行了另一个查询来插入请求的id,该id与第一个查询中插入的分析请求有关系。

现在如果我要插入很多,我会在数组中做一个数组并将每个记录插入其中吗?像这样。

$data = array(
        array(
            'client_id' => $input['client_id'],
            'sample_code' => $input['sample_code'],
            'sample_description' => $input['sample_description'],
            'quantity' => $input['quantity'],
            'analysis_requested' => $input['analysis_requested'],
            'special_instruction' => $input['special_instruction'],
            'status' => 'for_testing'
        ),
        // and so on.
    );

        AnalysisRequest::insert($data);

但我在上面的代码中有一个错误,它说数组转换为字符串

或者如果我为每个人做一个会更好吗?

我这里唯一的目标是执行插入许多查询取决于要添加的条目。

感谢有人可以提供帮助。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

您正在获取一个数组数组,并且您希望将(例如)放入client_id一个数组[1]中 出于这个原因,你有&#34;数组到字符串的转换&#34;。
你可以做到

$data = array(
    array(
        'client_id' => $input['client_id'][0],
        'sample_code' => $input['sample_code'][0],
        'sample_description' => $input['sample_description'][0],
        'quantity' => $input['quantity'][0],
        'analysis_requested' => $input['analysis_requested'][0],
        'special_instruction' => $input['special_instruction'][0],
        'status' => 'for_testing'
    ),
    // and so on.
);

AnalysisRequest::insert($data);