title: Laravel how to join tables and use where condition using Eloquent?
body: <p>Lets say there are 2 tables:</p>
  
  <p>videos (id, file_name)</p>
  
  <p>matches (id, videos_id, match_number)</p>
  
  <p>I want to get video file_name by match_number. I have read that using eloquent, you can avoid joins.</p>
  
  <p>So I try various things from examples like this:</p>
  
  <p>Video.php</p>
  
  <pre><code>class Video extends Eloquent {
  
      protected $table = 'videos';
  
      public function matches()
      {
          return $this-&gt;hasMany('Match', 'videos_id');
      }
  
      /*public function match()
      {
          return $this-&gt;belongsTo('Match', 'id');
      }*/
  
      /**
       * @param $matchNumber
       * @return mixed
       */
      public function getByMatchNumber($matchNumber)
      {
          return $this
              -&gt;matches()
              //-&gt;select('videos.file_name')
              //-&gt;limit(10)
              -&gt;where('matches.match_number', $matchNumber)
              -&gt;file_name;
               //   -&gt;match()
  
  
      }
  }
  </code></pre>
  
  <p>But nothing works.</p>
  
  <p>I think I am assuming well that video hasMany matches because in reality there are many matches which have same video.</p>
  
  <p>So how it should look?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Match_number column is unique, like id. There cannot be multiple matches with same match number.
  And there is only one video for one match. Limit(10) just was doing to try without where condition, to not get millions of results. 
  But same video is assigned to many matches.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I remade the function based on the jedrzej.kurylo answer and fixed mistakes I assume - whereMatchNumber - such function will not be existing:</p>
  
  <pre><code>public function getByMatchNumber($matchNumber)
      {
         return Match::with('video')
              -&gt;where('match_number', $matchNumber)-&gt;first(); //-&gt;video-&gt;file_name;
  
  
  
      }
  </code></pre>
  
  <p>But it does not work. I get error:</p>
  
  <p>Call to undefined method Illuminate\Database\Query\Builder::video()</p>
  
  <p><strong>Update</strong></p>
  
  <p>In Match.php model I added relation:</p>
  
  <pre><code>public function video()
      {
          return $this-&gt;belongsTo('Video', 'videos_id');
      }
  </code></pre>
  
  <p>Still getting the same error:</p>
  
  <p>Call to undefined method Illuminate\Database\Query\Builder::videos()</p>
  
  <p><strong>Update:</strong>
  I had a mistake, was trying to add plural form when gettign error and forgot to undo:</p>
  
  <pre><code>return Match::with('videos')
  </code></pre>
  
  <p>needed:</p>
  
  <pre><code>return Match::with('video')
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>Still not able to get result. I run this:</p>
  
  <pre><code>$video = $this-&gt;video-&gt;getByMatchNumber($this-&gt;matchNumber);
  
  print_r($video-&gt;file_name);
  </code></pre>
  
  <p>I see good queries in query log:</p>
  
  <pre><code> [1] =&gt; Array
          (
              [query] =&gt; select * from `matches` where `match_number` = ? limit 1
              [bindings] =&gt; Array
                  (
                      [0] =&gt; 104439
                  )
  
              [time] =&gt; 4.62
          )
  
      [2] =&gt; Array
          (
              [query] =&gt; select `file_name` from `videos` where `videos`.`id` in (?)
              [bindings] =&gt; Array
                  (
                      [0] =&gt; 1089
                  )
  
              [time] =&gt; 37.24
          )
  </code></pre>
  
  <p>I executed last query with the binding in HeidySQL and result is found. So why $video->file_name could be empty?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Also I tried </p>
  
  <pre><code>die($video-&gt;video-&gt;file_name);
  </code></pre>
  
  <p>and get error </p>
  
  <p><strong>Trying to get property of non-object</strong></p>