我想知道laravel chunk和laravel cursor方法之间有什么区别.哪种方法更适合使用?两者的用例是什么?我知道你应该使用游标来节省内存,但它在后端实际上是如何工作的?
通过示例的详细解释将是有用的,因为我已经搜索了stackoverflow和其他站点,但我没有找到太多信息.
以下是laravel文档中的代码片段.
分块结果
Flight::chunk(200, function ($flights) {
foreach ($flights as $flight) {
//
}
});
Run Code Online (Sandbox Code Playgroud)
使用游标
foreach (Flight::where('foo', 'bar')->cursor() as $flight) {
//
}
Run Code Online (Sandbox Code Playgroud) 我正在使用laravel,并且已经设置了抽象类方法来从正在调用的各种API中获得响应。但是,如果API网址不可访问,则会引发异常。我知道我想念什么。任何帮助对我来说都是很好的。
$offers = [];
try {
$appUrl = parse_url($this->apiUrl);
// Call Api using Guzzle
$client = new Client('' . $appUrl['scheme'] . '://' . $appUrl['host'] . '' . $appUrl['path']);
if ($appUrl['scheme'] == 'https') //If https then disable ssl certificate
$client->setDefaultOption('verify', false);
$request = $client->get('?' . $appUrl['query']);
$response = $request->send();
if ($response->getStatusCode() == 200) {
$offers = json_decode($response->getBody(), true);
}
} catch (ClientErrorResponseException $e) {
Log::info("Client error :" . $e->getResponse()->getBody(true));
} catch (ServerErrorResponseException $e) {
Log::info("Server error" . $e->getResponse()->getBody(true));
} catch (BadResponseException …Run Code Online (Sandbox Code Playgroud) 我想合并以下后续数组。我怎样才能做到这一点?条件:如果数组数等于3,则将后续数组合并到父级。
输入项
$data = [
['mango', 'orange', 'apple'],
['abc'],
['ctp'],
['pqr', 'cst', 'dtc'],
['cbx'],
['xyz'],
['atc'],
];
Run Code Online (Sandbox Code Playgroud)
我想要输出如下:
$data = [
['mango', 'orange', 'apple' 'additional' => [ ['abc'], ['ctp'] ] ],
['pqr', 'cst', 'dtc', 'additional' => [ ['cbx'],['xyz'],['atc'] ] ],
];
Run Code Online (Sandbox Code Playgroud)