Get the user name

Hey everyone,

These are my Tables in my DB.

when i open now for example tickets/view/4 it looks like this.

So i get the related ticket-snaps at the button.

and this is the code so far.

# TicketController.php
$ticket = $this->Tickets->get($id, contain: ['Users', 'Tags', 'TicketSnaps']);
# tickets/view.php
 <?php foreach ($ticket->ticket_snaps as $ticketSnap) : ?>
        <tr>
            <td><?= h($ticketSnap->id) ?></td>
            <td><?= h($ticketSnap->ticket_id) ?></td>
            <td><?= h($ticketSnap->user_id) ?></td>
            <td><?= h($ticketSnap->text) ?></td>
        </tr>
<?php endforeach; ?>

What do i have to change to get the user id replaced there by user name from the users table?
Somehow i have to take the user_id and ask the user table for the names.

??? ->get($ticketSnap->id)

Thank you :pray:

Use either
$ticket = $this->Tickets->get($id, contain: ['Users', 'Tags', 'TicketSnaps.Users']);
or my preference
$ticket = $this->Tickets->get($id, contain: ['Users', 'Tags', 'TicketSnaps' => ['Users']);

and then

<td><?= h($ticketSnap->user->first_name) ?></td>

1 Like

Perfect that works. Thank you so much. :pray: