Laravel中的变量save()返回的属性少于findOrFail

时间:2018-04-19 01:08:29

标签: laravel laravel-5 eloquent

我有一个控制器操作,它通过请求获取所有输入,并在提交操作之前将值放入类Shipment $shipment = Shipment::save();

但是,如果我返回dd($ shipment),我会得到以下属性(仅此而已):

"pro_number" => "231776"
    "shipment_origin" => "1"
    "date" => "2018-04-18 00:00:00"
    "due_date" => "2018-05-18 00:00:00"
    "tractor_id" => null
    "trailer_id" => null
    "driver_id" => null
    "notes" => null
    "shipper_no" => null
    "ship_to" => "200"
    "ship_from" => "200"
    "bill_to" => "200"
    "bill_type" => "1"
    "load_date" => null
    "shipment_status" => 1
    "shipment_billing_status" => 1
    "created_by" => 1
    "cn_billtoName" => null
    "cn_billtoAddress1" => null
    "cn_billtoAddress2" => null
    "cn_billtoCity" => null
    "cn_billtoState" => null
    "cn_billtoZip" => null
    "cn_billtoEmail" => null
    "cn_billtoPhone" => null
    "cn_shiptoName" => null
    "cn_shiptoAddress1" => null
    "cn_shiptoAddress2" => null
    "cn_shiptoCity" => null
    "cn_shiptoState" => null
    "cn_shiptoZip" => null
    "cn_shiptoEmail" => null
    "cn_shiptoPhone" => null
    "cn_shipfromName" => null
    "cn_shipfromAddress1" => null
    "cn_shipfromAddress2" => null
    "cn_shipfromCity" => null
    "cn_shipfromState" => null
    "cn_shipfromZip" => null
    "cn_shipfromEmail" => null
    "cn_shipfromPhone" => null
    "fuelChargeDesc" => null
    "fuelChargeAmt" => "0.00"
    "fuelChargeTotal" => "0.00"
    "permitChargeDesc" => null
    "permitChargeAmt" => "0.00"
    "permitChargeTotal" => "0.00"
    "otherChargeDesc" => null
    "otherChargeAmt" => "0.00"
    "otherChargeTotal" => "0.00"
    "noCharge" => null
    "noSettle" => null
    "Total" => "10.00"
    "Balance" => "10.00"
    "freightBillSubtotal" => "10.00"
    "updated_at" => "2018-04-18 18:20:11"
    "created_at" => "2018-04-18 18:20:11"
    "id" => 9739

当然,所有这些信息在技术上都是正确的。但是,它缺少一些字段,一个是我设置为在MySQL中记录插入时创建的UUID字段。

但是,如果我将以下内容更改为控制器操作的底部:

$shipmentDD = Shipment::findOrFail($shipment);
return dd($shipmentDD);

我收到三美元的运费,而不是上面唯一的运费。但是,三个中的一个是正确的发货,事实上确实在属性中有UUID字段。

有没有人对如何解决此问题有任何建议?我有一种感觉,因为我不是Laravel的专家,还在学习。

更新以包含货件和属性的屏幕截图: enter image description here

我的整个控制器动作:

$this->validate(request(), [

            'pro_number' => 'required',
            'shipment_origin' => 'required',
            'date' => 'required',
            'due_date' => '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
            $shipmentID = $shipment->id;

            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' => $shipmentID, 
                    '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 = 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//
            $shipment_details = $shipment->shipment_details;
            $shipmentAudits = $shipment->audits;
            $attachmentTypes = file_type::all();
            $attachmentVisibilities = visibility::all();
            $customerAccount = Auth::user()->customers;
            $freightBillSubtotal = $shipment_details->sum('charge');

return redirect('/shipments/show/'.$shipment->UUID)->with('create_code', 2);

1 个答案:

答案 0 :(得分:1)

是否在数据库中设置了UUID?如果是这样,您必须从数据库中获取该模型的新副本:

$shipment = Shipment::create($attributes)->fresh();