title: Laravel: Get column value from first() result of relationship query
body: <p>I'm trying to get a single column value from the first result of a Model's belongsToMany relationship query, as i'm returning the <code>-&gt;first()</code> result of the relationship I was hoping <code>$code-&gt;reward-&gt;title</code> would work but it doesn't.</p>
  
  <p>I get an <code>Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation</code> error</p>
  
  <p>What I'm trying to do is the get the title of the <em>current</em> reward that is linked to a specific code - the <code>code_reward</code> pivot table has a <code>valid_from</code> and <code>expires_at</code> date as the reward linked to a code will change as time goes by, hence the need to get the currently active reward for that code. </p>
  
  <p>Here's my code:</p>
  
  <p><strong>Model: Code</strong></p>
  
  <pre><code>public function rewards()
  {
      return $this-&gt;belongsToMany('App\Reward')-&gt;withPivot('valid_from', 'expires_at')-&gt;withTimestamps();
  }
  
  
  
  public function reward()
  {
      $now = Carbon::now();
      return $this-&gt;rewards()
          -&gt;wherePivot('valid_from', '&lt;', $now)
          -&gt;wherePivot('expires_at', '&gt;', $now)
          -&gt;first();
  }
  </code></pre>
  
  <p><strong>Controller: CodeController</strong></p>
  
  <pre><code>public function index()
      {
          $codes = Code::all();
          return view('codes/index')-&gt;with('codes', $codes);
      }
  </code></pre>
  
  <p><strong>View: Codes/index</strong></p>
  
  <pre><code>@foreach ($codes as $code)
  
      {{$code-&gt;id}}
      {{$code-&gt;reward-&gt;title}}
  
  @endforeach
  </code></pre>
  
  <p>Any help is really appreciated!</p>
  
  <p><strong>Update</strong></p>
  
  <p>Unfortunately both suggestions below (<code>$code-&gt;reward()-&gt;title</code> and <code>getRewardAttribute()</code> return an <code>Trying to get property of non-object</code> error. </p>
  
  <p>If I remove <code>-&gt;first()</code> from the Code->reward() method and replace <code>$code-&gt;reward-&gt;title</code> with <code>$code-&gt;reward-&gt;first()</code> in the view it echoes out the whole reward model as json, however <code>$code-&gt;reward-&gt;first()-&gt;title</code> still returns the <code>Trying to get property of non-object</code> error</p>
  
  <p><strong>Update 2</strong>
  If I do <code>{{dd($code-&gt;reward-&gt;title)}}</code> in the view I get the reward title but if I just do <code>{{$code-&gt;reward-&gt;title}}</code>, I don't!</p>
  
  <p>AND the <code>$code-&gt;reward-&gt;title</code> works as expected in a <code>@Show</code> view, so could it be that the collection of codes supplied by the controller's <code>@index</code> method isn't passing the necessary data or not passing it in a necessary format??</p>
  
  <p><strong>SOLVED</strong>
  The issue was caused by one of the <code>$code-&gt;reward</code>s in the <code>foreach</code> loop in the index view returning <code>null</code>! The first one didn't, hence the <code>dd()</code> working but as soon as the loop hit a <code>null</code> it crashed.
  Once I wiped and refreshed the db (and made sure my seeds where adding only valid data!) it worked. Doing <code>{{$code-&gt;reward ? $code-&gt;reward-&gt;title : ''}}</code> fixed the issue. Grrr. </p>