Answers.Body: <p>If you had a Matrix field with a handle of <code>matrix</code> and it had a Matrix Block Type with a handle of <code>MyMatrixBlock</code> and it had a Plain Text field with a handle of <code>text</code>, this is how you'd grab it in PHP.</p>
  
  <pre><code>$entry = Craft::$app-&gt;getEntries()-&gt;getEntryById(2);
  
  foreach ($entry-&gt;matrix as $matrixBlock) {
      if ($matrixBlock-&gt;getType()-&gt;name === 'MyMatrixBlock') {
          // Grab a text field from the block.
          $textField = $matrixBlock-&gt;text;
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-16T04:41:46.797Z
Answers.Text: Shouldn't it read "... with a name of MyMatrixBlock ..."? If you used the handle you'd need to change the code to "... getType()->handle === ...".
Answers.UserDisplayName: Crafty Cat
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-23T22:49:47.843Z
Answers.LastActivityDate: 2017-08-23T22:49:47.843Z
Answers.UserDisplayName: Brad Bell
Body: <pre><code>$entry = Entry::find()-&gt;section('my_section')-&gt;relatedTo($entry)-&gt;all();
  $entry['myMatrixField']; // What do I do with this??
  </code></pre>
  
  <p>I know there are tutorials about how to handle Matrix fields in plugins in Craft 2, but how do I do this in Craft 3?</p>
  
Tags: <matrix><craft3>
Title: How do you iterate over Matrix field in a Craft 3 plugin?
ThreadText: # How do you iterate over Matrix field in a Craft 3 plugin?
  
  (Asked by: username_0 on 2017-08-23)
  
  ```
  $entry = Entry::find()->section('my_section')->relatedTo($entry)->all();
  $entry['myMatrixField']; // What do I do with this??
  
  ```
  
  I know there are tutorials about how to handle Matrix fields in plugins in Craft 2, but how do I do this in Craft 3?
  
  **Tags:** matrix, craft3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-08-23. Score: 1 | Accepted answer
  
  If you had a Matrix field with a handle of `matrix` and it had a Matrix Block Type with a handle of `MyMatrixBlock` and it had a Plain Text field with a handle of `text`, this is how you'd grab it in PHP.
  
  ```
  $entry = Craft::$app->getEntries()->getEntryById(2);
  
  foreach ($entry->matrix as $matrixBlock) {
      if ($matrixBlock->getType()->name === 'MyMatrixBlock') {
          // Grab a text field from the block.
          $textField = $matrixBlock->text;
      }
  }
  
  ```
  
  > **Comments:**
  > **username_2**: Shouldn't it read "... with a name of MyMatrixBlock ..."? If you used the handle you'd need to change the code to "... getType()->handle === ...".  
  
  ---
  
  