title: Laravel - Select specific columns from joined relationship models using Eloquent
body: <p>I'm trying to select specific columns from tables that I have joined using Eloquent.</p>
  
  <p>I have 3 models
  - Transaction
  - Channel
  - Merchant</p>
  
  <p>Transactions links to Channel. It has a hasOne relationship.
  Channel links to Merchant. It also has a hasOne relationship.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public function channel() {
      return $this-&gt;hasOne(Channel::class, 'uuid', 'entityId');
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public function merchant() {
      return $this-&gt;hasOne('App\Merchant', 'uuid', 'sender');
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I'm using eager loading so have the following in the Transaction model: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>protected $with = ['channel'];</code></pre>
  </div>
  </div>
  </p>
  
  <p>And Channel has:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>protected $with = ['merchant']:</code></pre>
  </div>
  </div>
  </p>
  
  <p>This the query I'm trying to convert into Eloquent but I'm unsure how to select columns when they belong to related models. What I don't get is that if the relationships have been defined, why can't I select columns from the other models without having to reuse joins or the with clause?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>SELECT SUM(t.amount) AS amount, 
         m.name 
  FROM transactionsV2 t JOIN
       channels c
       ON t.entityId = c.uuid JOIN
       merchants m
       ON c.sender = m.uuid
  WHERE t.paymentType = 'DB' AND
        t.status = 1 AND
        t.processing_time &gt;= '2019-01-01' AND
        t.processing_time &lt; '2019-01-21'
  GROUP BY m.name;</code></pre>
  </div>
  </div>
  </p>