获取"完整性约束违规(密钥重复输入)"在Laravel

时间:2018-01-26 03:14:47

标签: php mysql laravel laravel-5

我有一个我最近编辑过的表,用两列添加另一个索引(组合起来必须是唯一的)。

现在问题是我在创建新的模型记录时遇到错误:

  

完整性约束违规:1062重复输入...用于密钥....

这两列标题为:

  • shipment_origin
  • pro_number

举个例子,最终结果如下:1-230185

我已经检查过,记录是从技术上创建的,它只是没有完成并且错误被返回。因此,为简单起见,事先没有记录(因此没有重复的条目),在数据库中创建记录并返回错误(无论如何)。

或者有没有办法直接通过Laravel而不是MySQL?

更新

以下是我的控制器中的公共功能:

public function store(Request $request)
    {

        $this->validate(request(), [

            'pro_number' => 'required',
            'shipment_origin' => 'required'
            /*'piecesNumber' => 'required' (had to remove for now, but must review)*/

        ]);

        $user_id = Auth::id();

        $input = $request->all();

        //Save Initial Shipment Data
        $shipment = new Shipment();
        $shipment->pro_number = request('pro_number');
        $shipment->shipment_origin = request('shipment_origin');
        $shipment->date = request('date');
        $shipment->due_date = request('due_date');
        $shipment->tractor_id = request('tractor_id');
        $shipment->trailer_id = request('trailer_id');
        $shipment->driver_id = request('driver_id');
        $shipment->notes = request('notes');
        $shipment->shipper_no = request('shipper_no');
        $shipment->ship_to = request('ship_to');
        $shipment->ship_from = request('ship_from');
        $shipment->bill_to = request('bill_to');
        $shipment->bill_type = request('bill_type');
        $shipment->load_date = request('load_date');
        $shipment->shipment_status = 1;
        $shipment->shipment_billing_status = (isset($request->shipment_billing_status) && !empty($request->shipment_billing_status)) ? $request->shipment_billing_status : 1;
        $shipment->created_by = $user_id;
        $shipment->cn_billtoName = request('cn_billtoName');
        $shipment->cn_billtoAddress1 = request('cn_billtoAddress1');
        $shipment->cn_billtoAddress2 = request('cn_billtoAddress2');
        $shipment->cn_billtoCity = request('cn_billtoCity');
        $shipment->cn_billtoState = request('cn_billtoState');
        $shipment->cn_billtoZip = request('cn_billtoZip');
        $shipment->cn_billtoEmail = request('cn_billtoEmail');
        $shipment->cn_billtoPhone = request('cn_billtoPhone');
        $shipment->cn_shiptoName = request('cn_shiptoName');
        $shipment->cn_shiptoAddress1 = request('cn_shiptoAddress1');
        $shipment->cn_shiptoAddress2 = request('cn_shiptoAddress2');
        $shipment->cn_shiptoCity = request('cn_shiptoCity');
        $shipment->cn_shiptoState = request('cn_shiptoState');
        $shipment->cn_shiptoZip = request('cn_shiptoZip');
        $shipment->cn_shiptoEmail = request('cn_shiptoEmail');
        $shipment->cn_shiptoPhone = request('cn_shiptoPhone');
        $shipment->cn_shipfromName = request('cn_shipfromName');
        $shipment->cn_shipfromAddress1 = request('cn_shipfromAddress1');
        $shipment->cn_shipfromAddress2 = request('cn_shipfromAddress2');
        $shipment->cn_shipfromCity = request('cn_shipfromCity');
        $shipment->cn_shipfromState = request('cn_shipfromState');
        $shipment->cn_shipfromZip = request('cn_shipfromZip');
        $shipment->cn_shipfromEmail = request('cn_shipfromEmail');
        $shipment->cn_shipfromPhone = request('cn_shipfromPhone');
        $shipment->fuelChargeDesc = request('fuelChargeDesc');
        $shipment->fuelChargeAmt = request('fuelChargeAmt');
        $shipment->fuelChargeTotal = request('fuelChargeTotal');
        $shipment->permitChargeDesc = request('permitChargeDesc');
        $shipment->permitChargeAmt = request('permitChargeAmt');
        $shipment->permitChargeTotal = request('permitChargeTotal');
        $shipment->otherChargeDesc = request('otherChargeDesc');
        $shipment->otherChargeAmt = request('otherChargeAmt');
        $shipment->otherChargeTotal = request('otherChargeTotal');
        $shipment->noCharge = request('noCharge');
        $shipment->noSettle = request('noSettle');
        $shipment->Total = request('Total');
        if ((request('shipment_billing_status') == 2) || (request('shipment_billing_status') == 3)){
           $balance = 0.00;
        }else{
           $balance = request('Total');
        }
        $shipment->Balance = $balance;
        $shipment->freightBillSubtotal = request('freightBillSubtotal');

        $shipment->save();

        //Save Shipment Details

        for ($i = 0; $i < count($request->shipment_details['piecesNumber']); $i++) {
            //the first line used to be 'shipment_id' => $shipment->pro_number,
            Shipment_Detail::create([
                'shipmentID' => $shipment->id, 
                'pieces_number' => $request->shipment_details['piecesNumber'][$i],
                'pieces_type' => $request->shipment_details['piecesType'][$i],
                'rate_type' => $request->shipment_details['rateType'][$i],
                'charge' => $request->shipment_details['charge'][$i],
                'weight' => $request->shipment_details['weight'][$i],
                'hazmat' => $request->shipment_details['hazmat'][$i],
                'description' => $request->shipment_details['description'][$i] ]);
        }

        $carrier = \App\Customer::where('carrier','=',1)->get();
        foreach($carrier as $car){
                        $carrierUsers = $car->users;
                        if ($carrierUsers->count() > 0){
                            foreach($carrierUsers as $carrierUser){
                                $carrierUser->notify(new FreightBillNew($shipment));
                            }
                }
                }  

        Session::flash('success_message','Freight Bill Successfully Created'); //<--FLASH MESSAGE

        //Return to Register//
        return redirect('/shipments/accounts');

    }

0 个答案:

没有答案