title: Laravel Eloquent nested relations returns data only on the first element
body: <h1>Intro</h1>
  
  <p>Im havning some trubble getting the data on all the related elements. Im using Laravel as a REST backend service, exposing Json to the front-end javascript application.</p>
  
  <h2>Data structure</h2>
  
  <p>Consider I have the following tables:</p>
  
  <pre><code>+----------------+ +----------------+ +-------------+
  |topics          | |posts           | |users        |
  +----------------+ +----------------+ +-------------+
  |id: int         | |id: int         | |id: int      |
  |title: varchar  | |content: varchar| |name: varchar|
  |content: varchar| |user_id: int    | +-------------+
  |user_id: int    | |topic_id: int   |
  +----------------+ +----------------+ 
  </code></pre>
  
  <p>A topic has 0 to many posts, and it has an author (user)</p>
  
  <p>A post has one author (user)</p>
  
  <p>UML: <a href="http://i58.servimg.com/u/f58/11/26/57/95/intrep10.png" rel="noreferrer">http://i58.servimg.com/u/f58/11/26/57/95/intrep10.png</a></p>
  
  <h2>Laravel Models</h2>
  
  <pre><code>class User extends Eloquent {
      protected $table = 'users';
  
      public function topics() {
          reutrn $this-&gt;hasMany('Topic');
      }
  
      public function posts() {
          reutrn $this-&gt;hasMany('Post');
      }
  }
  
  class Topic extends Eloquent {
      protected $table = 'topics';
  
      public function posts() {
              return $this-&gt;hasMany('Post');
      }
  
      public function author() {
           return $this-&gt;hasOne('User', 'id');
      }
  }
  
  class Post extends Eloquent {
      protected $table = 'posts';
  
      public function topic() {
          return $this-&gt;belongsTo('Topic');
      }
  
      public function author() {
          return $this-&gt;hasOne('User', 'id');
      }
  }
  </code></pre>
  
  <h2>Controller</h2>
  
  <pre><code>return Topic::where('id', '=', $topicId)
      -&gt;with('author', 'posts.author')
      -&gt;get();
  </code></pre>
  
  <h2>Output</h2>
  
  <pre><code>[{
      id: 1,
      title: "My Topic",
      content: "With opinions about the darkside",
      user_id: 1,
      created_at: "2014-03-06",
      updated_at: "2014-03-06",
      author: {
          id: 1,
          name: "JamesBond",
          created_at: "2014-03-06",
          updated_at: "2014-03-06",
      },
      posts: [{
          id: 1,
          content: "Reply 1 on topic 1",
          user_id: 1,
          created_at: "2014-03-06",
          updated_at: "2014-03-06",
          author: {
              id: 1,
              name: "JamesBond",
              created_at: "2014-03-06",
              updated_at: "2014-03-06",
          },
      },
      {
          id: 2,
          content: "Reply 2 on topic 1",
          user_id: 1,
          created_at: "2014-03-06",
          updated_at: "2014-03-06",
          author: null,
      }]
  }]
  </code></pre>
  
  <h2>Question</h2>
  
  <p>As you can see of the jsoncode, both the posts are created by the same user (with id 1), but only the first has the author object on it.
  Any pointers on how to figure out my problem would be perfect.</p>
  
  <h2>Disclaimer</h2>
  
  <p>This is a striped down version of my project, as I dont want to spam the question with information. If I lack serten elements to the question, I will be happy to supply it.</p>
  
  <h2>Solution</h2>
  
  <p>My model mapping was off.</p>
  
  <pre><code>public function author() {
      return $this-&gt;belongsTo('User', 'user_id', 'id');
  }
  </code></pre>
  
  <p>makes sure that its the user_id its looking for in the posts table, up against the id column in the users table</p>
  
  <pre><code>SELECT * FROM users WHERE id = posts.user_id;
  </code></pre>