title: Laravel global scopes and joins
body: <p>I have a global scope setup in Laravel 5.1 which is working fine.  However on some of my pages I am using MySQL <code>joins</code> using the Eloquent builder.  This results in an ambiguous error:</p>
  
  <pre><code>Column 'cust_id' in where clause is ambiguous
  </code></pre>
  
  <p>I'm not sure how to avoid this problem.  I know that I can use sub queries instead, but is there no other solution?</p>
  
  <p>Here is my scope file:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Scopes;
  
  use Illuminate\Database\Eloquent\Model;
  use Illuminate\Database\Eloquent\Builder;
  use Illuminate\Database\Eloquent\ScopeInterface;
  use App\Customers;
  use DB, Session;
  
  class MultiTenantScope implements ScopeInterface
  {
      /**
       * Create a new filter instance.
       *
       * @param  UsersRoles $roles
       * @return void
       */
      public function __construct()
      {
          $this-&gt;custId = Session::get('cust_id');
      }
  
      /**
       * Apply scope on the query.
       *
       * @param Builder $builder
       * @param Model $model
       * @return void
       */
      public function apply(Builder $builder, Model $model)
      {
          if($this-&gt;custId) 
          {
              $builder-&gt;whereCustId($this-&gt;custId); 
          } 
          else 
          {
              $model = $builder-&gt;getModel();
              $builder-&gt;whereNull($model-&gt;getKeyName()); 
          }
      }
  
      /**
       * Remove scope from the query.
       *
       * @param Builder $builder
       * @param Model $model
       * @return void
       */
      public function remove(Builder $builder, Model $model)
      {
          $query = $builder-&gt;getQuery();
          $query-&gt;wheres = collect($query-&gt;wheres)-&gt;reject(function ($where) 
          {
              return ($where['column'] == 'cust_id');
          })-&gt;values()-&gt;all();
      }  
  }
  </code></pre>