title: Querying Multiple Joins for Laravel Nova Lens
body: <p>I am trying to figure out how to create a query for a Laravel Nova Lens. I know the underlying query is effectively what is important here, but I can't figure that out either. I am trying to retrieve values from a relationship, here are my tables:</p>
  
  <pre><code>accounts
  statements
  statement_versions
  </code></pre>
  
  <p>What I want to do is retrieve a value from the <code>statement_versions</code> table, and associate it with an account in the lens. So, I want to retrieve the <code>balance</code> from my <code>statement_versions</code>, and associate it with an account. Here are how my relationships are defined:</p>
  
  <pre><code>// Accounts
  
  public function statements()
  {
      return $this-&gt;hasMany(Statement::class);
  }
  
  public function statementVersions()
  {
      return $this-&gt;hasManyThrough(StatementVersion::class, Statement::class);
  }
  
  // Statements
  
  public function accounts()
  {
      return $this-&gt;belongsTo(Account::class);
  }
  
  public function statementVersions()
  {
      return $this-&gt;hasMany(StatementVersion::class);
  }
  
  // StatementVersion
  
  public function accounts()
  {
      return $this-&gt;belongsToThrough(Account::class, Statement::class);
  }
  
  public function statements()
  {
      return $this-&gt;belongsTo(Statement::class);
  }
  </code></pre>
  
  <p>Here is the lens I have attempted to create:</p>
  
  <pre><code>public static function query(LensRequest $request, $query)
  {
      return $request-&gt;withOrdering($request-&gt;withFilters(
          $query-&gt;select([
                      'statement_version.balance',
                  ])
                  -&gt;join('statements', 'statement.id', '=', 'statement_version.statement_id')
                  -&gt;join('accounts', 'accounts.id', '=', 'statement.account_id')
                  -&gt;orderBy('balance', 'desc')
                  -&gt;groupBy('accounts.id', 'accounts.name')
      ));
  }
  </code></pre>
  
  <p>This returns an error <code>1066 Not unique table/alias: 'accounts'</code>. How would I make this query and this lens work?</p>