title: Laravel Eloquent one-to-many relationship
body: <p>I am having real problems getting Laravel's Eloquent ORM to return data for a relationship.</p>
  
  <h1>My migrations (database schema):</h1>
  
  <pre><code>Schema::table('users', function($table)
  {
      $table-&gt;engine = 'InnoDB';
      $table-&gt;create();
  
      $table-&gt;increments('id')-&gt;unsigned();
      $table-&gt;string('firstname');
      $table-&gt;string('surname');
      $table-&gt;string('email')-&gt;unique();
      $table-&gt;string('password')-&gt;unique();
      $table-&gt;string('phone')-&gt;nullable();
      $table-&gt;text('about')-&gt;nullable();
      $table-&gt;timestamps();
  });
  
  Schema::table('files', function($table)
  {
      $table-&gt;engine = 'InnoDB';
      $table-&gt;create();
  
      $table-&gt;increments('id')-&gt;unsigned();
      $table-&gt;string('filename');
      $table-&gt;string('title');
      $table-&gt;text('description')-&gt;nullable();
      $table-&gt;string('keywords')-&gt;nullable();
      $table-&gt;integer('category_id')-&gt;unsigned();
      $table-&gt;integer('file_type_id')-&gt;unsigned();
      $table-&gt;integer('user_id')-&gt;unsigned();
      $table-&gt;timestamps();
  });
  </code></pre>
  
  <h1>Models</h1>
  
  <h2>File.php</h2>
  
  <pre><code>&lt;?php
  
  class File extends Eloquent
  {
  
      public static $timestamps = true;
  
      public function user()
      {
          return $this-&gt;belongs_to('User');
      }
  
  }
  </code></pre>
  
  <h2>User.php</h2>
  
  <pre><code>&lt;?php
  
  class User extends Eloquent
  {
      public static $timestamps = true;
  
      public function files()
      {
          return $this-&gt;has_many('File');
      }
  }
  </code></pre>
  
  <h2>routes.php</h2>
  
  <pre><code>// everything else in my routes.php is as it was when downloaded
  Route::get('users', function()
  {
      echo '&lt;pre&gt;';
  
      // this works
      $users = User::all();
      print_r($users);
  
      // this doesn't work
      print_r($users-&gt;files);
  
      // this doesn't work
      $files = User::find(1)-&gt;files;
      print_r($files);
  });
  </code></pre>
  
  <h1>Errors:</h1>
  
  <blockquote>
    <p>Unhandled Exception</p>
    
    <p>Message:</p>
    
    <p>Trying to get property of non-object
      Location:</p>
    
    <p>C:\wamp\www\l3_mlib\application\routes.php on line 51
      Stack Trace:</p>
    
    <p>#0 C:\wamp\www\l3_mlib\laravel\laravel.php(42): Laravel\Error::native(8, 'Trying to get p...', 'C:\wamp\www\l3_...', 51)
      #1 C:\wamp\www\l3_mlib\application\routes.php(51): Laravel{closure}(8, 'Trying to get p...', 'C:\wamp\www\l3_...', 51, Array)
      #2 [internal function]: {closure}()
      #3 C:\wamp\www\l3_mlib\laravel\routing\route.php(163): call_user_func_array(Object(Closure), Array)
      #4 C:\wamp\www\l3_mlib\laravel\routing\route.php(124): Laravel\Routing\Route->response()
      #5 C:\wamp\www\l3_mlib\laravel\laravel.php(167): Laravel\Routing\Route->call()
      #6 C:\wamp\www\l3_mlib\public\index.php(34): require('C:\wamp\www\l3_...')
      #7 {main}</p>
  </blockquote>
  
  <p>What am I doing wrong?</p>