Create Matrix Block via Module I'm having an issue creating a matrix block from a module. I'm currently doing the following:


Calling an API using Guzzle
Getting that request and iterating over a media block.
Trying to create a new Matrix Block with each iteration.
Adding data from the request into each Matrix Block.


** EDIT **

I got this working using the following code:

                $data = json_decode($json, true);
                $number = 1;

                foreach($data['media'] as $element) {
                  $matrixData = array();
                  $matrixData["new$number"] = array(
                    'type' =&gt; 'media',
                    'enabled' =&gt; true,
                    'fields' =&gt; array(
                      'mediaDescription' =&gt; $element['likes'],
                    )
                  );
                  $user-&gt;setFieldValue('recentPosts', $matrixData);
                  $number++;
                }


Example JSON

"media": [
        {
            "media_id": "2053313765945828720",
            "instagram_user_id": "1",
            "shortcode": "Bx-1f2_nuVw",
            "created_date": "1558994096",
            "caption": "",
            "comments": "7077",
            "likes": "1131719",
            "media_url": "https://www.instagram.com/p/Bx-1f2_nuVw",
            "media_image_url": "https://scontent-lhr3-1.cdninstagram.com/vp/8e6c054cec7d3c3657bc73d49dae0fd9/5D7B651B/t51.2885-15/e35/c0.134.1080.1080/s150x150/61993210_834852143539503_2474404095511077560_n.jpg?_nc_ht=scontent-lhr3-1.cdninstagram.com",
            "type": "IMAGE",
            "mentions": [],
            "hashtags": [],
            "date": "2019-05-28 02:44:09",
            "last_check_date": "2019-05-28 20:25:24"
        },
...


Unfortunately, now my forloop isn't saving new matrix blocks.
Fixed using the following code. Also includes fetching an API via Guzzle and parsing it into Craft CMS. Thought I would post here so that if anyone else is having trouble they can decipher bits from this. 

                // Set a base URI
                $baseUri = 'http://domain.com';
                // Build the Guzzle Client and use the Base URI
                $client = Craft::createGuzzleClient([
                  'base_uri' =&gt; $baseUri,
                ]);
                // Send a Request!
                $response = $client-&gt;request('GET', '/mytestendpoint/');
                // Parse the Response
                $json = $response-&gt;getBody();
                // Decode it
                $data = json_decode($json, true);

                // Set some values from it!
                $user-&gt;setFieldValue('userTotalFollowers', $data['followers'] );
                $user-&gt;setFieldValue('userEngagementRating', $data['details']['total_likes'] );
                $user-&gt;setFieldValue('verified', $data['is_verified'] );

                $number = 0;
                $matrixData = array();

                foreach($data['media'] as $element) {
                  $number++;
                  $matrixData["new$number"] = array(
                    'type' =&gt; 'media',
                    'enabled' =&gt; true,
                    'fields' =&gt; array(
                      'mediaDescription' =&gt; $element['likes'],
                      'mediaLikes' =&gt; count($element),
                    )
                  );
                }
                $user-&gt;setFieldValue('recentPosts', $matrixData);