Level 102
This is something that should be done on a queue or using a command. A http request is meant to return quickly
i am trying to fetch products from the API through guzzleHttp client. there is almost 14204 products when i start fetching and saving into my database almost 6708 products will fetched but then i got request timeout error. my code will be like this in the controller function.
public function syncproducts()
{
$erpdata = Helper::geterpvalues();
$client = new Client();
$res = $client->get($erpdata->erp_api_link.'/products?limit=15000', [
\GuzzleHttp\RequestOptions::HEADERS => array(
'debug' => true,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'APIKEY' => $erpdata->erp_api_key
)]);
if ($res->getStatusCode()==200) {
$products = json_decode($res->getBody());
if ($products->data) {
// dd($products->data);
foreach ($products->data as $product) {
$categoryId = null;
if ($product->Product->ProductCategory) {
$categoryId = $product->Product->ProductCategory[0]->id;
}
//$matchThese = ['product_id'=>$product->Product->id];
if (Product::where('product_id', '=', $product->Product->id)->where('product_code', '=', $product->Product->product_code)->count()== 0) {
Product::Create(['product_id'=>$product->Product->id,'product_code'=>$product->Product->product_code]);
}
}
$detail = [
'message' => 'New Products is updated',
];
$users = User::where('type', '=', 'Ship Owner')->get();
Notification::send($users, new SyncProductNotification($detail));
return redirect()->route('products.index')
->with('success', 'Data Synced Successfully');
}
} else {
return redirect()->route('products.index')
->with('destroy', 'There is an error in fetching data.');
}
}
Please or to participate in this conversation.