title: Laravel - Eloquent Relationship hasMany but also hasOne?
body: <p>I've been all over the web and struggling with this for around 2 hours.</p>
  
  <p>I have a USER model, a RUN model and a TIME model.</p>
  
  <p>In the real world the user is in a race and gets their time entered in the database along with a USER_id and a RUN_id.</p>
  
  <p>A user should only be able to have one row in the TIMES table for each RUN_id - if that makes sense!</p>
  
  <p>Is this something I need to manage at the controller level? Or is there a relationship I can setup to ensure that a duplicate entry of this style can not enter the database?</p>
  
  <p>Database structure at present:</p>
  
  <hr>
  
  <p>USERS:</p>
  
  <p><strong>name</strong></p>
  
  <hr>
  
  <p>RUNS:</p>
  
  <p><strong>name</strong></p>
  
  <hr>
  
  <p>TIMES:</p>
  
  <p><strong>time</strong></p>
  
  <p><strong>user_id</strong></p>
  
  <p><strong>run_id</strong></p>
  
  <hr>
  
  <p>The Models:</p>
  
  <p>USER:</p>
  
  <pre><code>public function times()
  {
      return $this-&gt;hasMany(Time::class);
  }
  </code></pre>
  
  <p>RUN: </p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Run extends Model
  {    
      public function user()
      {
          return $this-&gt;belongsTo(User::class);
      }
  
      public function times()
      {
          return $this-&gt;hasMany(Time::class);
      }
  }
  </code></pre>
  
  <p>TIME:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Time extends Model
  {
      public function user()
      {
          return $this-&gt;belongsTo(User::class);
      }
  
      public function run()
      {
          return $this-&gt;belongsTo(Run::class);
      }
  
  }
  </code></pre>