# Get Related Record ID Value

**URL:** <https://discourse.cakephp.org/t/get-related-record-id-value/12425>\
**Category:** Need Help\
**Tags:** help\
**Created:** [February 16, 2025, 12:17pm UTC](https://discourse.cakephp.org/t/get-related-record-id-value/12425 "2025-02-16T12:17:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Foggies](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@Foggies](https://discourse.cakephp.org/u/Foggies)\
**Post date:** [February 16, 2025, 12:17pm UTC](https://discourse.cakephp.org/t/get-related-record-id-value/12425/1 "2025-02-16T12:17:50Z")

</div>

Hello guys,

I’m using CakePHP 5. I have a few tables:

1. Webs (id)
2. Audits (id, web\_id, session\_id, user\_id)
3. Sessions (id, name)
4. Users (id, fullname)

When I open web views, for example …/webs/view/1, it will show the related audits. In the related audit (view in webs), it has user\_id and session\_id, which show the number, e.g., user\_id == 1 and session\_id == 1. The user table has a fullname column, and the session table has a name column. How can I render the fullname and session name in the related record view on the web view?

Thank you, and I appreciate any assistance from the forum members.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [February 16, 2025, 12:29pm UTC](https://discourse.cakephp.org/t/get-related-record-id-value/12425/2 "2025-02-16T12:29:38Z")

</div>

you just output the field you want in the associated records.

`bin/cake bake all --everything` generates something like this:

```php
<div class="related">
    <h4><?= __('Related Sessions') ?></h4>
    <?php if (!empty($web->sessions)) : ?>
    <div class="table-responsive">
        <table>
            <tr>
                <th><?= __('Id') ?></th>
                <th><?= __('Name') ?></th>
                <th><?= __('Created') ?></th>
                <th><?= __('Modified') ?></th>
                <th class="actions"><?= __('Actions') ?></th>
            </tr>
            <?php foreach ($web->sessions as $session) : ?>
            <tr>
                <td><?= h($session->id) ?></td>
                <td><?= h($session->name) ?></td> // <=========
                <td><?= h($session->created) ?></td>
                <td><?= h($session->modified) ?></td>
                <td class="actions">
                    <?= $this->Html->link(__('View'), ['controller' => 'Sessions', 'action' => 'view', $session->id]) ?>
                    <?= $this->Html->link(__('Edit'), ['controller' => 'Sessions', 'action' => 'edit', $session->id]) ?>
                    <?= $this->Form->postLink(__('Delete'), ['controller' => 'Sessions', 'action' => 'delete', $session->id], ['confirm' =>__('Are you sure you want to delete # {0}?', $session->id)]) ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </table>
    </div>
    <?php endif; ?>
</div>

```

`$web->sessions` contains all the associated Sessions records which are mapped to your current web entry. This automatically happens if you do `$this->Webs->get($id, ['contain' => ['Sessions']])` inside your controller action - its called [Eager Loading](https://book.cakephp.org/5/en/orm/retrieving-data-and-resultsets.html#eager-loading-associations-via-contain)

`$session` inside the foreach is just an entity of type `App\Model\Entity\Session` and therefore represents the associated record in your current web entry.

---

<div class="post-metadata">

**Author:** ![Foggies](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@Foggies](https://discourse.cakephp.org/u/Foggies)\
**Post date:** [February 26, 2025, 1:24pm UTC](https://discourse.cakephp.org/t/get-related-record-id-value/12425/3 "2025-02-26T13:24:11Z")

</div>

Sorry for the late reply. Thanks for the solution. It solved my problem. Here i share the solution:

webs controller (view):

```auto
...
$web = $this->Webs->get($id, [
            'contain' => ['Audits' => ['Users', 'Sessions']],
        ]);

```

in webs view related:

```auto
<?php foreach ($web->audits as $audits) : ?>
<tr>
<td><?= h($audits->session_id) ?>: <?= h($audits->session->name) ?></td>
<td><?= h($audits->user->fullname) ?></td>
...

```
