get($url, ['q' => 'iphone', 'limit' => $limit, 'skip' => $skip]); if($response->successful()){ $json = $response->json(); $products = $response->json('products'); $skip = $response->json('skip'); $total = $response->json('total'); $this->upsertProducts($products); $skip += $limit; $this->info('Received ' . count($products) . ' records'); } else { $this->error('Error on HTTP request!'); } } while($skip < $total); $this->info('Total: ' . $total); } protected function upsertProducts($products):void { foreach ($products as $product){ $tags = $product['tags']; $reviews = $product['reviews']; $images = $product['images']; $product['width'] = $product['dimensions']['width']; $product['height'] = $product['dimensions']['height']; $product['depth'] = $product['dimensions']['depth']; $product['barcode'] = $product['meta']['barcode']; $product['qr_code'] = $product['meta']['qrCode']; unset($product['tags'], $product['reviews'], $product['images'], $product['dimensions'], $product['meta'] ); $product = $this->arrayKeysCamelToSnake($product); Product::query()->updateOrInsert(['id' => $product['id']], $product); $p = Product::find($product['id']); // detach all tags and attach received $p->tags()->detach(); $p->tags()->attach($this->getTagsIds($tags)); // delete all reviews and create from received ProductReview::query()->where('product_id', '=', $p->id)->delete(); foreach ($reviews as $review){ $review['product_id'] = $p->id; ProductReview::query()->create($this->arrayKeysCamelToSnake($review)); } ProductImage::query()->where('product_id', '=', $p->id)->delete(); foreach ($images as $image){ ProductImage::create(['product_id' => $p->id, 'url' => $image]); } } } protected function getTagsIds($tags):array { $ids = []; foreach ($tags as $tag){ $t = Tag::query()->firstOrCreate(['name' => $tag]); $ids[] = $t->id; } return $ids; } protected function arrayKeysCamelToSnake($arr): array { $ret = []; foreach ($arr as $k => $v) $ret[Str::snake($k)] = $v; return $ret; } }