Laravel不会将数据从数据库填充到Web表单

时间:2018-06-07 03:53:40

标签: jquery laravel

我正在学习使用Laravel 5.6和JQuery创建一个网站。该网站能够进行CRUD处理。但是,当我使用以下代码更新按钮时,将显示模型而不从数据库中填充数据。

当我运行此代码时,控制台面板不会显示任何内容。

Edit does not populate records

table schema

仪表板中使用以下代码

dashboard.blade.php

<script>
    $(document).on("click", ".editagent", function()
    {
        $('#edagid').val($(this).data('aid'));
        $('#edagfname').val($(this).data('edfname'));
        $('#edaglname').val($(this).data('edlname'));
        $('#edagmail').val($(this).data('edemail'));
        $('#edagaddress').val($(this).data('edaddress'));
        $('#edagphn1').val($(this).data('edphn1'));
        $('#edagphn2').val($(this).data('edphn2'));
        $('#edagnic').val($(this).data('ednic'));
        $('#edagbrnc').val($(this).data('edbrnc'));
        $('#edagactive').val($(this).data('edactive'));

        $('#agenteditmodel').modal('show');
    });

    $("#btneditagentupdate").click(function()
    {
        var data = new FormData();
        var id = $("#edagid").val();
        console.log('agent id = ', id);

        data.append('id', $("#edid").val());
        data.append('policy_Id', $("#edpcid").val());
        data.append('name', $("#edname").val());
        data.append('description', $("#eddescription").val());
        data.append('amount', $("#edamount").val());
        data.append('duration', $("#edduration").val());
        data.append('_token', $("#tokenedit").val());

        $.ajax(
        {
            type: "post",
            data: data,
            cache: false,
            processData: false,
            contentType: false,
            url: "<?php  echo url('/editagentdata') ?>",
            success: function(data)
            {
                console.log(data);
                if (data.error)
                {
                    $('#newerroredit').text(data.error[0]);
                    $("#errmsgedit").show();
                    return false;
                }
                console.log(data);

                $("#errmsgedit").hide();

                $('#brc' + id).replaceWith("<tr id='brc" + data.id + "'><td>" + data.id + "</td><td>" + data.policyId + "</td><td>" + data.name + "</td><td>" + data.description + "</td><td>" + data.amount + "</td><td>" + data.duration + "</td><td><button type='button' class='btn btn-info btn-xs editpolicycover' data-duration=" + data.duration + " data-amount=" + data.amount + " data-descr=" + data.description + " data-name=" + data.name + " data-pcid=" + data.policyId + " data-id=" + id + "><i class='fa fa-edit'></i> Edit</button> <button type='button' class='btn btn-info btn-xs viewbranch' data-descr=" + data.description + " data-contact2=" + data.contactNo2 + " data-contact1=" + data.contactNo1 + " data-address=" + data.address + " data-name=" + data.name + " data-id=" + id + "><i class='fa fa-eye'></i> View</button> <button type='button' class='btn btn-danger btn-xs delbranch'  data-id=" + id + "><i class='fa fa-trash'></i> Delete</button></td></tr>");

                $('#agenteditmodel').modal('toggle');

                swal(
                {
                    title: "Success!",
                    text: "Data Updated Successfully",
                    icon: "success",
                    button: "OK",
                });

            },
            error: function(json)
            {
                $.LoadingOverlay("hide");
                swal(
                {
                    title: "Error!",
                    text: "Sorry! Something Wrong in data",
                    icon: "error",
                    button: "OK",
                });
            }
        });

    });
</script>

以下代码用于编辑代理

editagent.blade.php

<!-- Modal -->
<div class="modal fade" id="agenteditmodel" role="dialog">
   <div class="modal-dialog">
      <!-- Modal content-->
      <div class="modal-content">
         <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Modal Header</h4>
         </div>
         <div class="modal-body">
            <div id="errmsgedit" class="alert alert-warning" style="display:none"><span id='newerroredit'>
               <strong>Warning!</strong> Indicates a warning that might need attention.
               </span>
            </div>
            <form role="form" id="Branchdata">
            <input type="hidden" name="publictockenedit" id="tokenedit" value="{{csrf_token()}}" required="required" class="form-control col-md-7 col-xs-12">
            <input type="hidden" name="publictockenedit" id="editagentid" value="" >
            <div class="box-body">
               <div class="form-group">
                  <div class="row">
                     <label for="exampleInputEmail1" class="control-label col-md-3 col-sm-3 col-xs-12" >Agent ID</label>
                     <div class="col-md-9 col-sm-9 col-xs-12">
                        <input type="text" class="form-control" id="edaid" placeholder="Enter Id">
                     </div>
                  </div>
               </div>
               <div class="form-group">
                  <div class="row">
                     <label for="exampleInputPassword1" class="control-label col-md-3 col-sm-3 col-xs-12">First Name</label>
                     <div class="col-md-9 col-sm-9 col-xs-12">
                        <input type="text" class="form-control" id="edfname" placeholder="name">
                     </div>
                  </div>
               </div>
               <div class="form-group">
                  <div class="row">
                     <label for="exampleInputPassword1" class="control-label col-md-3 col-sm-3 col-xs-12">Last Name</label>
                     <div class="col-md-9 col-sm-9 col-xs-12">
                        <input type="text" class="form-control" id="edlname" placeholder="Last name">
                     </div>
                  </div>
               </div>
               <div class="form-group">
                  <div class="row">
                     <label for="exampleInputPassword1" class="control-label col-md-3 col-sm-3 col-xs-12">Email</label>
                     <div class="col-md-9 col-sm-9 col-xs-12">
                        <input type="text" class="form-control" id="edemail" placeholder="Email">
                     </div>
                  </div>
               </div>
               <div class="form-group">
                  <div class="row">
                     <label for="exampleInputPassword1" class="control-label col-md-3 col-sm-3 col-xs-12">Address</label>
                     <div class="col-md-9 col-sm-9 col-xs-12">
                        <input type="text" class="form-control" id="edaddress" placeholder="address">
                     </div>
                  </div>
               </div>
               <div class="form-group">
                  <div class="row">
                     <label for="exampleInputPassword1" class="control-label col-md-3 col-sm-3 col-xs-12">Phone Number</label>
                     <div class="col-md-9 col-sm-9 col-xs-12">
                        <input type="text" class="form-control" id="edphn1" placeholder="Phone Number">
                        <br/>
                        <input type="text" class="form-control" id="edphn2" placeholder="Phone Number">
                     </div>
                  </div>
               </div>
               <div class="form-group">
                  <div class="row">
                     <label for="exampleInputPassword1" class="control-label col-md-3 col-sm-3 col-xs-12">NIC No</label>
                     <div class="col-md-9 col-sm-9 col-xs-12">
                        <input type="text" class="form-control" id="ednic" placeholder="Enter your NIC number">
                     </div>
                  </div>
               </div>
               <div class="form-group">
                  <div class="row">
                     <label for="exampleInputPassword1" class="control-label col-md-3 col-sm-3 col-xs-12">Branch</label>
                     <div class="col-md-9 col-sm-9 col-xs-12">
                        <select class="form-control" id="edbrnc">
                           @foreach($branch as $value)
                           <option value="{{$value->id}}">{{$value->name}}</option>
                           @endforeach
                        </select>
                     </div>
                  </div>
               </div>
               <div class="form-group">
                  <label for="middle-name" class="control-label col-md-3 col-sm-3 col-xs-12">Active</label>
                  <div class="col-md-6 col-sm-6 col-xs-12">
                     <input type="checkbox" value=1 name="new1" id="edactive" class="flat" checked="checked">
                  </div>
               </div>
            </div>
            <!-- /.box-body -->
         </div>
         <div class="modal-footer">
            <button type="button" id="btneditagentupdate" class="btn btn-primary">Submit</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
         </div>
      </div>
   </div>
</div>

此代码是代理控制器 的 agentController.blade.php

public function update(Request $request)
    {
      $validator = Validator::make($request->all(),[
        'name' => 'required|max:25',
        'address' => 'required|max:145',
        'contact_No1' => 'required|max:10',
        'contact_No2' => 'max:10',
        'description' => 'max:255',
       ]);

      if($validator->passes()) {
      $agent = Branch::find($request->agent_Id);
      $agent->name = $request->name;
      $agent->address = $request->address;
      $agent->contactNo1 = $request->contact_No1;
      $agent->contactNo2 = $request->contact_No2;
      $agent->description = $request->description;
      $agent->save();
      return response()->json($agent);
      }
      return response()->json(['error'=>$validator->errors()->all()]);
    }

0 个答案:

没有答案