Laravel,有没有办法将整个表与数组同步?

有一个客户端 - 服务器的 API 调用,它返回大约 600 的发票,因为它们没有优化,所以需要最多 2.3 分钟的时间。

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

之后,我将记录存储在我的数据库中以跟踪发票并添加评论和发送提醒。

http://img.mukewang.com/61c52d2a000113fe11280476.jpg

问题是有一种情况,如果发票变成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']]);

            }

        }


墨色风雨
浏览 171回答 2
2回答

翻翻过去那场雪

您想更新所有收到的发票的信息,但将唯一未支付的发票保存到$this->filteredResults. 您可以在$this->saveInvoices()方法中移动“过滤器逻辑” ,这将解决问题。$response = collect(\GuzzleHttp\json_decode($response->getBody()->getContents()));$this->saveInvoices($response);public function saveInvoices($invoices){&nbsp; &nbsp; foreach ($invoices as $item) {&nbsp; &nbsp; &nbsp; &nbsp; //If it wasn't paid and dueDate not passed, then save it to $this->filteredResults&nbsp; &nbsp; &nbsp; &nbsp; if(($item['paid'] === false) && ($item["dueDate"] <= $now)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $collection = collect($item);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($this->filteredResults, $collection->only(['invoiceNumber', 'customerId', 'customerName', 'dueDate', 'invoiceDate', 'amountInOriginalCurrency', 'paid'])->toArray());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //Then update invoice information, regardless of paid status&nbsp; &nbsp; &nbsp; &nbsp; UnpaidInvoices::updateOrCreate(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ['invoiceNumber' => $item['invoiceNumber']],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ['invoiceNumber' => $item['invoiceNumber'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'customerId' => $item['customerId'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'customerName' => $item['customerName'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'dueDate' => $item['dueDate'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'invoiceDate' => $item['invoiceDate'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'amountInOriginalCurrency' => $item['amountInOriginalCurrency'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'paid' => $item['paid']]);&nbsp; &nbsp; }}

慕运维8079593

确保您在 DB 中设置了良好的索引。仅从后端返回您真正需要的内容(前端没有过滤)在检索集合/数组后,更新记录更改应是一个单独的调用,可能最简单的方法是再次调用以获取最新结果(第 1 点)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript