<p>figured this one out on my own, so to get all of the related objects, I just needed to change the above query to <code>$buildings = $location-&gt;buildings-&gt;all();</code></p>

CC BY-SA 4.0
2018-05-30T04:00:17.757Z
2018-05-30T04:00:17.757Z
Brett Moore
<p>So I've got this which accurately gives me the desired relation block within my matrix, but how do I actually get the Entry object from a relation field?</p>

<pre><code>$project = Entry::find()-&gt;id($projectId)-&gt;one();

foreach ($project-&gt;location as $locations) {
        if ($locations-&gt;getType()-&gt;name === 'Locations') {
            // Grab a building from the block. 
            // Right now this just returns the relation table ID, I'd like the actual Entry object with it's attributes.
            $buildings = $locations-&gt;buildings;
        }

    }
</code></pre>

CC BY-SA 4.0
2018-05-29T20:11:50.187Z
Hey there - figured this one out on my own, so to get all of the related objects, I just needed to change the above query to `$buildings = $location->buildings->all();`
Brett Moore
CC BY-SA 4.0
2018-05-30T00:44:40.793Z
Would you mind adding that solution as an official answer, Brett?  Might end up helping someone in the future. :)
Brad Bell
<plugin-development><plugins>
Get the related entry from an Entries field in a Matrix block within a plugin craft 3
# Get the related entry from an Entries field in a Matrix block within a plugin craft 3

(Asked by: username_0 on 2018-05-29)

So I've got this which accurately gives me the desired relation block within my matrix, but how do I actually get the Entry object from a relation field?
$project = Entry::find()->id($projectId)->one();

foreach ($project->location as $locations) {
        if ($locations->getType()->name === 'Locations') {
            // Grab a building from the block. 
            // Right now this just returns the relation table ID, I'd like the actual Entry object with it's attributes.
            $buildings = $locations->buildings;
        }

    }

**Tags:** plugin-development, plugins
> **Comments:**
> **username_0**: Hey there - figured this one out on my own, so to get all of the related objects, I just needed to change the above query to `$buildings = $location->buildings->all();`  
> **username_1**: Would you mind adding that solution as an official answer, Brett?  Might end up helping someone in the future. :)  

---

## 1 answers

### Answer by username_0 on 2018-05-30. Score: 2

figured this one out on my own, so to get all of the related objects, I just needed to change the above query to `$buildings = $location->buildings->all();`


---

