【问题标题】:Strange error Undefined index "Item"奇怪的错误未定义的索引“项目”
【发布时间】:2023-03-18 11:17:01
【问题描述】:

我收到此错误。很奇怪,因为索引存在。

这是数组:

array(8) {
 ["OrderID"]=>
   string(6) "201646"
 ["FolioNo"]=>
   string(12) "540840840840"
 ["InvoiceID"]=>
   string(3) "277"
 ["CreatedOn"]=>
   object(Illuminate\Database\Query\Expression)#1252 (1) {
     ["value":protected]=>
        string(9) "GETDATE()"
     }
 ["CreatedBy"]=>
   string(1) "4"
 ["Item"]=>
   string(8) "Lien New"
 ["ItemDescriptions"]=>
   string(8) "Lien New"
 ["ItemPrice"]=>
   string(6) "150.00"
}

与 $data 的 print_r 共享屏幕截图

当我尝试使用 Laravel 中的下一行在数据库中搜索该记录时引发错误。

$itemExist = InvoiceItem::where('OrderID', $data['OrderID'])->where('Item', $data['Item'])->pluck('ItemID');

Var $data 是在不同的步骤中创建的,当系统需要创建一种 item 时...

private function createRushInvoiceItem($order){
    if($order->IsRUSHOrder == 1){
        $data = $this->getExpensesData($order);
        $data = array_merge($data, [
            'Item' => "Lien RUSH",
            'ItemDescriptions' => "Lien RUSH",
            'ItemPrice' => $this->getClientPrice($order, 3)
        ]);

        $this->createInvoiceItem($data);
    }
}

首先,我正在设置通用值:

public function getExpensesData($order) {
    $data = [
        "OrderID" => $order->OrderID,
        "FolioNo" => $order->Property->FolioNo,
        'InvoiceID' => $this->getInvoiceID($order->OrderID),
    ];

    # Getting timeStamps for this record on create
    $data = $this->setTimeStamps($data, 'C');
    return $data;
}

public function setTimeStamps($data, $section) {
    if($section == 'C') {
         $data = array_merge($data, [
            'CreatedOn' => DB::raw('GETDATE()'),
            'CreatedBy' => Auth::user()->UserID
        ]);
    } else {
        $data = array_merge($data, [
            'UpdatedOn' => DB::raw('GETDATE()'),
            'UpdatedBy' => Auth::user()->UserID
        ]);
    }
    return $data;
}

最后我正在创建项目,这是出现错误的时间

public function createInvoiceItem($data) {
    $itemExist = InvoiceItem::where('OrderID', $data['OrderID'])->where('Item', $data['Item'])->pluck('ItemID');

    try {
        return !is_null($itemExist)? InvoiceItem::where('ItemID', $itemExist)->update($data) : InvoiceItem::create($data);
    }
    catch (Expection $exc) {
        return $exc->getMessage();
    }
}

【问题讨论】:

  • 代码没有问题。可以在定义 $data 的地方添加代码吗?
  • 好人!我已经做到了!
  • 你说得对,我已经被var_dump催眠了。
  • hehehehe 这就是我喜欢 print_r() 大多数时候不需要更多信息的方式。我尝试获取该查询的结果......这对我来说是一个 X 文件......当我 print_r($itemExist); 查询后,我可以看到正确的结果。

标签: php arrays laravel laravel-4


【解决方案1】:

伙计们,我不得不改变流程的方式来创建不同的项目来修复它。更多代码行,但正在运行。

还是谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多