如何在laravel mvc中使用数组存储多个数据?

时间:2017-08-10 13:48:14

标签: arrays laravel

  

我正在研究mvc我对OOP概念知之甚少。我在用   laravel为我的第一个演示项目。我有add more的1个表单   按钮。 onclick add more按钮下一个相同的表格将同时打开   div的值应该存储在表中。我想存储数据   分开的行。如果我通过这种方式,它就不会在桌面上存储任何东西。如何   我们可以存储所有数据吗?

表格:

<div class="form-group box" id="fieldList">
  {!! Form::label('address',trans('messages.address'),[])!!}
  {!! Form::input('text','address_line_1',(isset($company) ? $company->address_line_1 : ''),['class'=>'form-control','placeholder'=>trans('messages.address_line_1')])!!}
                    <br />
  {!! Form::input('text','address_line_2',(isset($company) ? $company->address_line_2 : ''),['class'=>'form-control','placeholder'=>trans('messages.address_line_2')])!!}
                    <br />
<div class="row">
 <div class="col-xs-5">
  {!! Form::input('text','city',(isset($company) ? $company->city : ''),['class'=>'form-control','placeholder'=>trans('messages.city')])!!}
</div>
<div class="col-xs-4">
 {!! Form::input('text','state',(isset($company) ? $company->state : ''),['class'=>'form-control','placeholder'=>trans('messages.state')])!!}
</div>
<div class="col-xs-3">
 {!! Form::input('text','zipcode',(isset($company) ? $company->zipcode : ''),['class'=>'form-control','placeholder'=>trans('messages.zipcode')])!!}
</div>                      
</div>                  
                    <br />
 {!! Form::select('country_id', config('country'),(isset($company) ? $company->country_id : ''),['class'=>'form-control show-tick','title'=>trans('messages.country')])!!}
                    <br />

 {!! Form::label('gstn',trans('messages.gstn'),[])!!}
 {!! Form::input('text','gstn',(isset($company) ? $company->gstn : ''),['class'=>'form-control','placeholder'=>trans('messages.gstn')])!!}
</div>
</div>
 {{ getCustomFields('company-form',$custom_field_values) }}
 {!! Form::submit(isset($buttonText) ? $buttonText : trans('messages.save'),['class' => 'btn btn-primary pull-right']) !!}
<input class="button-add btn btn-primary" type="button" value="Add Another Address">

克隆以上div的脚本:

<script type="text/javascript">
    $(document).ready(function() {

    $('.button-add').click(function(){

        $('.box:first').clone().insertAfter(".box:last");
    });

    $(document).on("click", ".button-remove", function() {
        $(this).closest(".box").remove();
    });
});
</script>

存储方式:

public function store(CompanyRequest $request, Company $company, Companyaddress $companyaddress){   

        if(!Entrust::can('create-company'))
            return response()->json(['message' => trans('messages.permission_denied'), 'status' => 'error']);

        $data = $request->all();
        $company->fill($data)->save();
        $companyaddress->fill($data)->save();
        storeCustomField($this->form,$company->id, $data);

        $this->logActivity(['module' => 'company_address','module_id' => $company->id,'activity' => 'added']);

        return response()->json(['message' => trans('messages.company').' '.trans('messages.added'), 'status' => 'success']);
    }

型号:

class Companyaddress extends Eloquent {

    protected $fillable = [
                            'companies_id',
                            'address_line_1[]',
                            'address_line_2[]',
                            'city[]',
                            'state[]',
                            'zipcode[]',
                            'country_id[]',
                            'gstn[]'                            
                        ];
    protected $primaryKey = 'id';
    protected $table = 'company_address';
}

0 个答案:

没有答案