title: Eloquent join with scopes having ambiguous column names
body: <p>I've got two tables, <code>venues</code> and <code>venue_plans</code>, having a <code>hasOne</code> relationship. The former contains geolocation data, while the latter contains a "distance bonus", so the more the distance bonus, the closer the venue will be shown to the user who's searching around its location.</p>
  
  <p>There's also a third table, <code>categories</code>, which has a <code>hasMany</code> relationship. Basically, each venue can have more than one categories set, as simple as that.</p>
  
  <p>I have managed to add to scopes: the first one calculates the distance from a given location (for example, the user location), calculates the distance bonus and sort the venues by both fields:</p>
  
  <pre><code>public function scopeWithDistanceFrom($query, $lat, $lng, $units = 'km') {
      $units = ($units === "km") ? 6378.10 : 3963.17;
      $lat = (float) $lat;
      $lng = (float) $lng;
      $lat_column = 'geo_latitude';
      $lng_column = 'geo_longitude';
  
      // Join with venue_plans to get the distance bonus
      $query-&gt;leftJoin('venue_plans', 'venues.id', 'venue_plans.venue_id');
  
      // Add distance column
      $distance_raw = "$units * ACOS(
                          COS(RADIANS($lat)) * COS(RADIANS($lat_column))
                          * COS(RADIANS($lng) - RADIANS($lng_column))
                          + SIN(RADIANS($lat)) * SIN(RADIANS($lat_column))
                      ) AS distance";
      $query-&gt;selectRaw($distance_raw);
  
      // Add distance_bonused column
      $distance_bonused_raw = "(SELECT (distance - (distance / 100 * distance_bonus))) as distance_bonused";
      $query-&gt;selectRaw($distance_bonused_raw);
  
      // Sort by distance
      $query-&gt;orderBy('distance_bonused', 'desc');
      $query-&gt;orderBy('distance', 'desc');
  
      return $query;
  }
  </code></pre>
  
  <p>The second one searches venues by their name (<code>venue.name</code>) or by the category name (<code>categories.name</code>).</p>
  
  <pre><code>public function scopeWithNameOrCategoryName($query, $name) {
      return $query
          -&gt;where('name', 'like', "%{$name}%") // Venue name
          -&gt;orWhereHas('categories', function($query) use ($name){ // Category name
              $query-&gt;where('name', 'like', "%{$name}%");
          });
  }
  </code></pre>
  
  <p>While they both work fine when used separately, when I use them together, like with</p>
  
  <pre><code>Venue::withNameOrCategory('myname')
      -&gt;withDistanceFrom(35.0386, 13.92)
      -&gt;take(5)-&gt;get();
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'name' in where clause is ambiguous (SQL: select `venues`.*, 6378.1 * ACOS(COS(RADIANS(35.0386)) * COS(RADIANS(geo_latitude)) * COS(RADIANS(13.92) - RADIANS(geo_longitude)) + SIN(RADIANS(35.0386)) * SIN(RADIANS(geo_latitude))) AS distance, (SELECT (distance - (distance / 100 * distance_bonus))) as distance_bonused from `venues` left join `venue_plans` on `venues`.`id` = `venue_plans`.`venue_id` where (`name` like %myname% or exists (select * from `categories` inner join `category_venue` on `categories`.`id` = `category_venue`.`category_id` where `venues`.`id` = `category_venue`.`venue_id` and `categories`.`name` like %myname%)) order by `distance_bonused` desc, `distance` desc limit 5)
  </code></pre>
  
  <p>I should mention that even the <code>venue_plans</code> table has a <code>name</code> column, to store the selected plan name and keep it specific for that venue.</p>
  
  <p>Can somebody explain to me what's the cleanest method to do this? I would also avoid to use table names and joins if possible.</p>
  
  <p>Thanks</p>