Laravel,有什么办法可以将整个表与数组同步吗?

时间:2019-09-03 07:49:25

标签: php laravel eloquent

有一个client-server的API调用,该API会返回大约600的发票,由于未进行优化,它最多需要花费2.3分钟的时间。

所以有一个条件,我需要使用paid: false过滤数据,我也这样做了。

此后,我将记录存储在数据库中,以跟踪发票,添加注释并发送提醒。

I have to save reminders and comments with current Model

问题在于,如果发票变成Paid: true,那么在过滤器中它会被忽略,所以我将如何更新我的表,因为在那儿它将保持为假。


    $response = collect(\GuzzleHttp\json_decode($response->getBody()->getContents()));
            $filtered = $response->filter(function ($value, $key) use ($now) {
                return ($value->paid == false && $value->dueDate <= $now);
            });
            $this->saveInvoices($filtered);



    public function saveInvoices($filtered)
        {
            foreach ($filtered as $val) {
                $item = collect($val);
                array_push($this->filteredResults, $item->only(['invoiceNumber', 'customerId', 'customerName', 'dueDate', 'invoiceDate', 'amountInOriginalCurrency', 'paid'])->toArray());
            }

            foreach ($this->filteredResults as $item) {

                UnpaidInvoices::updateOrCreate(
                    ['invoiceNumber' => $item['invoiceNumber']],
                    ['invoiceNumber' => $item['invoiceNumber'],
                        'customerId' => $item['customerId'],
                        'customerName' => $item['customerName'],
                        'dueDate' => $item['dueDate'],
                        'invoiceDate' => $item['invoiceDate'],
                        'amountInOriginalCurrency' => $item['amountInOriginalCurrency'],
                        'paid' => $item['paid']]);
            }
        }

2 个答案:

答案 0 :(得分:0)

  1. 确保在数据库中设置了正确的索引。
  2. 仅从后端返回真正需要的内容(前端不过滤)
  3. 关于记录更改的更新应在检索集合/数组后进行单独调用,可能最简单的方法是随后进行另一个CALL以获取最新结果(第1点)。

答案 1 :(得分:0)

您想更新所有收到的发票中的信息,但将唯一未支付的发票保存到$this->filteredResults。 您可以在$this->saveInvoices()方法内移动“过滤器逻辑”,这将解决问题。

$response = collect(\GuzzleHttp\json_decode($response->getBody()->getContents()));
$this->saveInvoices($response);


public function saveInvoices($invoices)
{
    foreach ($invoices as $item) {
        //If it wasn't paid and dueDate not passed, then save it to $this->filteredResults
        if(($item['paid'] === false) && ($item["dueDate"] <= $now)){
            $collection = collect($item);
            array_push($this->filteredResults, $collection->only(['invoiceNumber', 'customerId', 'customerName', 'dueDate', 'invoiceDate', 'amountInOriginalCurrency', 'paid'])->toArray());
        }

        //Then update invoice information, regardless of paid status
        UnpaidInvoices::updateOrCreate(
            ['invoiceNumber' => $item['invoiceNumber']],
            ['invoiceNumber' => $item['invoiceNumber'],
             'customerId' => $item['customerId'],
             'customerName' => $item['customerName'],
             'dueDate' => $item['dueDate'],
             'invoiceDate' => $item['invoiceDate'],
             'amountInOriginalCurrency' => $item['amountInOriginalCurrency'],
             'paid' => $item['paid']]);
    }
}