Monday, May 22, 2017

How to submit a collection through Laravel + Guzzle?

I am trying to send a collection of products in which I select only products with status = 1. I've done the SQL query through Laravel, but the only way I found it to send was through the foreach, the API I'm trying to implement has a time-out between inserts.That is, I need to send one by one of the query.

Here is my code to select the products:

$product = Product::where('erp_status', '=', 1)->limit(1)->offset(0)->get();

Here I try to manipulate the data through a foreach:

foreach($product as $prod){
          $r = $client->request('POST', 'https://api.mercadolibre.com/items?access_token=XXXXXXXXXXX', [
              'json' => [
                  'title' => substr($prod->description->erp_name, 0, 60),
                  'category_id' => 'MLB46511',
                  'price' => 10,
                  'currency_id' => 'BRL',
                  'available_quantity' => 10,
                  'buying_mode' => 'buy_it_now',
                  'listing_type_id' => 'gold_special',
                  'condition' => 'new',
                  'description' => 'Teste',
                  'pictures' => [
                      ['source' => $prod->image->erp_image]
                  ]
              ]
          ]);
      }

Is there any way I can transform the collection into an object and then manipulate the data one by one for the range to be respected?

If yes, how?

Any suggestion?



via Vinicius Rosa

Advertisement