# Get userdetails in template view

**URL:** <https://discourse.cakephp.org/t/get-userdetails-in-template-view/4389>\
**Category:** Need Help\
**Created:** [June 10, 2018, 8:52am UTC](https://discourse.cakephp.org/t/get-userdetails-in-template-view/4389 "2018-06-10T08:52:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [June 10, 2018, 8:52am UTC](https://discourse.cakephp.org/t/get-userdetails-in-template-view/4389/1 "2018-06-10T08:52:14Z")

</div>

I’m trying to get the userdetails (at the moment just the `firstname` and the `lastname`) from the currently logged in user.  
However, these details are **not** stored in the same table as the login details (`username`, `email` and `password`).  
This is how my schema looks (please ignore the fact that I use a `1:n` relation, this is now a `1:1` relation):  
 ![schema](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/6/697187ce3c2ba4019818c49cba6e0ffdbea36565.png)

I’m trying to get the `firstname` and the `lastname` into my view (`nav.ctp`) which is currently loaded into the template using `<?= $this->element('nav') ?>` in the `Template/Layout/default.ctp` file.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [June 10, 2018, 10:47am UTC](https://discourse.cakephp.org/t/get-userdetails-in-template-view/4389/2 "2018-06-10T10:47:45Z")

</div>

how do you get user from your database?

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [June 10, 2018, 1:29pm UTC](https://discourse.cakephp.org/t/get-userdetails-in-template-view/4389/3 "2018-06-10T13:29:05Z")

</div>

Currently, they login on the login page, and the content from the `users` table gets put into the session (with the exception of the `password hash` of course).  
currently I don’t get the `users_details` contents at all because I want this to be fetched when the user loads the page instead of having to re log.  
this then has to be loaded and echo’ed into the `nav.ctp` in a specific spot (which says `Welcome, <firstname + lastname>`).

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [June 10, 2018, 1:59pm UTC](https://discourse.cakephp.org/t/get-userdetails-in-template-view/4389/4 "2018-06-10T13:59:04Z")

</div>

I added this code to my `AppController.php`:

```
if($this->request->getSession()->read('Auth.User')){
  $this->loadModel('UsersDetails');
  $user_details = $this->UsersDetails->find(
    'all',
    array(
      'conditions' => array(
        'id' => $this->request->getSession()->read('Auth.User')['id']
      )
    )
  )->toArray();
  $this->request->getSession()->write('Auth.UserDetails',$user_details);
}

```

And this to my `nav.ctp`:

```
<?php if($this->request->getSession()->read('Auth.User')){ ?>
  <a class="dropdown-item" href="#">Welcome, <?= htmlentities($this->request->getSession()->read('Auth.UserDetails')[0]->firstname); ?></a>
<?php } else { ?>
  <a class="dropdown-item" href="/login">Login</a>
  <a class="dropdown-item" href="/register">Register</a>
<?php } ?>

```

This seems to have done the trick 🙂  
It might not be the cleanest solution (I think?), but for now I can continue.  
If somebody has advice for me on how to do this better, then feel free to add that to the thread

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [June 10, 2018, 3:06pm UTC](https://discourse.cakephp.org/t/get-userdetails-in-template-view/4389/5 "2018-06-10T15:06:55Z")

</div>

few pointers to your code (hope you wont get me as a picky one 😉 )

`$this->request->getSession()->read('Auth.User')` is the same as `$this->Auth->user()` you can also get fields from user when adding argument to is like `$this->Auth->user('id')`

instead of `$this->request->getSession()->read('Auth.User')` in view template its better to set user in controller

```auto
$this->set('loggedIn', $this->Auth->user());

//in template
if ($loggedIn) { ...

```

`htmlentities` in cake you have shothand `h` function i.e. `echo h($loggedIn['username']);`

try to familiarize yourself with cake [https://book.cakephp.org/3.0/en/views/helpers/html.html](https://book.cakephp.org/3.0/en/views/helpers/html.html) instead of

```auto
<a class="dropdown-item" href="/login">Login</a>

```

do

```auto
<?= $this->Html->link('Login', ['controller' => 'Users', 'action' => 'login'], ['class' => 'dropdown-item']) ?>

```

this ways if your route changes in future it will be adjusted automaticly

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [June 10, 2018, 3:52pm UTC](https://discourse.cakephp.org/t/get-userdetails-in-template-view/4389/6 "2018-06-10T15:52:16Z")

</div>

Thanks for the feedback, I’ll change this right away 🙂  
I knew about the `Html helpers` but I just keep forgetting them (still have to get used to it).
