title: How to use eloquent to compare a column with a related table column
body: <p>Let's say you have search result page that can display cars by a number of criteria, including year released.</p>
  
  <p>When the user doesn't specify a year, I want to pull all cars ("year_models") from the latest available year in each brand. Eg, you would see 2019 Chevys, but 2009 Pontiacs.  In each brand table, I have a "latest_year" column that can be used for this. </p>
  
  <p>Relationships: </p>
  
  <ul>
  <li>brands (eg, Chevrolet, Ford, etc..)</li>
  <li>--brand_models (Corvette, Silverado,etc.)  </li>
  <li>----year_models (eg, 2019 Corvette)  </li>
  </ul>
  
  <p>Each brand has many brand_models. Each brand model has many year_models. </p>
  
  <p>How do I perform this query? Here is my code so far:</p>
  
  <pre><code>$year = $request['year'];
  
  $yearModels = YearModel::with([
      'brandModel',
      'brandModel.brand',
  ])
  -&gt;when(!$year, function ($q) {
      $q-&gt;whereHas('brandModel.brand', function ($q) use ($year) {
          $q-&gt;where('latest_year', $YEARMODEL_YEAR?? );
      });
  })
  ...
  </code></pre>
  
  <p>I also tried a "wherecolumn" query but I don't think I can query the brand relationship that way. </p>