title: How can I add a join to a custom exists rule for a laravel validator?
body: <p>The validator in laravel can have a customization of the exists database rule, for instance if you need to check an extra column. An example from <a href="https://laravel.com/docs/5.5/validation#rule-exists" rel="noreferrer">the manual</a>:</p>
  
  <pre><code>use Illuminate\Validation\Rule;
  
  Validator::make($data, [
      'email' =&gt; [
          'required',
          Rule::exists('staff')-&gt;where(function ($query) {
              $query-&gt;where('account_id', 1);
          }),
      ],
  ]);
  </code></pre>
  
  <p>The <code>query</code> in the closure is not typehinted, so i'm not quite positive what kind of object this is. I can see that the <code>DatabaseRule</code> itself only has some function about <code>where</code>, <code>wherenot</code> etc, but i'm looking to add a join to the mix.</p>
  
  <p>Given example says that the email must exist for a staff with <code>account_id = 1</code>, but what if the team (all staff are part of a team, which is a separte table) should have a certain property, e.g. <code>team.active = 1</code> ?<br>
  <sup>the whole staff/team thing is an example of course</sup></p>
  
  <hr>
  
  <p>So in the end I'm wondering: <strong>how can I add a join to this rule so we make sure that the staff's team has a column 'active' that is 1.</strong> </p>
  
  <p>My first issue might be: <strong>what is the type of that <code>$query</code>?</strong> I would imagine that something like this would be great, but there's no reason to suspect this is valid:</p>
  
  <pre><code>Rule::exists('staff')-&gt;where(function ($query) {
      $query
          -&gt;join('team', 'team.team_id', '=', 'staff.team_id')
          -&gt;where('team.active', 1);
  })
  </code></pre>
  
  <p>This does not seem to work. The strange thing is that the join itself does not cause an error, but seems to be ignored:</p>
  
  <blockquote>
    <p>Column not found:<br>
    1054 Unknown column 'team.active' in 'where clause'<br>
    (SQL: select count(*) as aggregate from <code>staff</code> where <code>email</code> = -thevalue- and (<code>team</code>.<code>active</code> = 1))</p>
  </blockquote>
  
  <p>I would have expected this to work (small change as I'm gambling that this function is available here), or get an error because I'm calling a non-existent function. But what I get is a query that is build but without the join.</p>
  
  <p>From the comments I added a <code>$query-&gt;toSQL()</code> to that function. This does show a rather expected result, but it does not compute with the error I'm getting:</p>
  
  <pre><code>select * from `staff`
  inner join `teams` on `teams`.`team_id` = `staff`.`team_id`
  where `teams`.`active` = ?
  </code></pre>