如何解决双重插入数据库的问题

时间:2019-04-11 02:15:37

标签: php ajax laravel server

当我在本地网络中的程序仅一次插入时,我有插入问题。当我将程序放入服务器时,插入是两次。是因为版本xampp,laravel版本还是我的代码

这在控制器代码中

public function store(Request $request)
    {
      if ($request->ajax()) {

        if($request->groupName == ''){
            return response()->json([
            "status"  => "Failed",
            "message" => "Group Name Must Be Filled"
          ]);
          exit;
        }

        Group::create([
          'dealerId' => Session::get('dealerId'),
          'name'     => $request->groupName,
          'fee'      => 0.1
        ]);

        Log::create([
          'operator_id' => Session::get('userId'),
          'menu_id'     => '15',
          'action_id'   => '3',
          'date'        => Carbon::now('GMT+7'),
          'description' => 'Create new Group with name '.$request->groupName
        ]);


        return response()->json([
          "status"  => "OK",
          "message" => "Group Created"
        ]);

      }
    }

可以查看此输入

<div id="createGroup" class="black-box modal hide fade">
<form id="frmTable" action="{{ route('createGroup') }}">
  <div class="modal-header tab-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <span>Create Group</span>
  </div>
  <div class="modal-body separator">
    <div class="note">Please enter the new group name.</div>

    <p>
        <input type="text" name="groupName" id="name" class="input-transparent" placeholder="Group name">
    </p>
    <p>
        <input type="text" name="groupName" id="name" class="input-transparent" placeholder="Group name">
    </p>
    <p>
        <input type="text" name="groupName" id="name" class="input-transparent" placeholder="Group name">
    </p>
  </div>
  <div class="modal-footer">
    <div class="inner-well">
      <a class="button mini rounded light-gray" data-dismiss="modal">Cancel</a>
      <a id="btnCreateGroup" class="button mini rounded blue">Create Group</a>
    </div>
  </div>
    </form>
</div>


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

      $.ajaxSetup({
          headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          }
      });


            $(".deleteGroup").click(function(e) {
               e.preventDefault();

                $("#btnDeleteGroup").attr('data-pk',$(this).data('pk'));
                $("#deleteGroup").modal('show');

            });


            $("#btnDeleteGroup").click(function(e) {
               e.preventDefault();

                $(".close").click();

               var id = $(this).attr('data-pk');
         var url = '{{ route('deleteGroup') }}';

               $.ajax ({
                    type: 'POST',
                    url: url,
                    data: {name:'delete',pk:id},
                    dataType: "json",
                    success: function(response) {  // here you receive response from you serverside

                        if(response.status == "OK") {
                            setTimeout(function() {
                                location.reload();
                            }, 1000);

                            return Notifications.push({
                            imagePath: "/images/images/cloud.png",
                            text: response.message,
                            time: "just now"
                          });
                        } else {
                            return Notifications.push({
                                imagePath: "/images/images/cloud.png",
                                text: response.message,
                                time: "just now"
                              });
                        }
                    }
                });

            });

            $("#btnCreateGroup").click(function(e) {
               e.preventDefault();

                var data = $("#frmTable").serialize();
        var url = $(this).attr('action');


        $.ajax ({
                    type: 'POST',
                    url: url,
                    data: data,
                    dataType: "json",
                    success: function(response) {  // here you receive response from you serverside
                        $(".close").click();
                        $("#amount").val('');

                        if(response.status == "OK") {

                            setTimeout(function() {
                                location.reload();
                            }, 1000);

                            return Notifications.push({
                            imagePath: "/images/images/cloud.png",
                            text: response.message,
                            time: "just now"
                          });
                        } else {
                            return Notifications.push({
                                imagePath: "/images/images/cloud.png",
                                text: response.message,
                                time: "just now"
                              });
                        }
                    }
                });

            });

这在模型组中

class Group extends Model
{
    protected $guarded = [];
    protected $table = 'groups';

    public $timestamps = false;

    public function percentGroupFee(){
      $fee = $this->fee * 100;
      return $fee;
    }
}

这是日志模型

class Log extends Model
{
    protected $guarded = [];
    protected $table = 'admin_log';

    public $timestamps = false;
}

但是我不知道哪一个是错误的

0 个答案:

没有答案