title: How to select custom attributes fields with relation in Laravel?
body: <p>I have tables <code>emails</code> and <code>email_attachments</code> as follows:</p>
  <pre><code>Schema::create('emails', function (Blueprint $table) {
      $table-&gt;id();
      $table-&gt;string('email')-&gt;nullable(false);
      $table-&gt;string('subject')-&gt;nullable(false);
      $table-&gt;text('body')-&gt;nullable(false);
      $table-&gt;timestamps();
  });
  
  Schema::create('email_attachments', function (Blueprint $table) {
      $table-&gt;id();
      $table-&gt;foreignId('email_id')-&gt;nullable(false);
      $table-&gt;string('file_name')-&gt;nullable(false);
      $table-&gt;string('file_path')-&gt;nullable(false);
  
      $table-&gt;foreign('email_id')-&gt;references('id')-&gt;on('emails');
  });
  </code></pre>
  <p>And my models classes <code>Email</code> and <code>EmailAttachment</code> as follows:</p>
  <pre><code>class Email extends Model
  {
      use HasFactory;
  
      protected $fillable = ['email', 'subject', 'body'];
  
      function attachments()
      {
          return $this-&gt;hasMany(EmailAttachment::class);
      }
  }
  
  class EmailAttachment extends Model
  {
      use HasFactory;
  
      public $timestamps = false;
      protected $fillable = ['email_id', 'file_name', 'file_path'];
      protected $appends = ['url'];
  
      function email()
      {
          return $this-&gt;belongsTo(Email::class);
      }
  
      public function getUrlAttribute()
      {
          return Storage::disk('public')-&gt;url($this-&gt;file_name);
      }
  }
  </code></pre>
  <p>Doing this <code>return Email::with('attachments')-&gt;get();</code> I get everything as response:</p>
  <pre><code>{
      &quot;id&quot;: 1,
      &quot;email&quot;: &quot;me@me.com&quot;,
      &quot;subject&quot;: &quot;My Subject&quot;,
      &quot;body&quot;: &quot;&lt;html&gt;&lt;body&gt;&lt;h1&gt;My test message.&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;&quot;,
      &quot;created_at&quot;: &quot;2021-02-26T23:32:08.000000Z&quot;,
      &quot;updated_at&quot;: &quot;2021-02-26T23:32:08.000000Z&quot;,
      &quot;attachments&quot;: [
          {
              &quot;id&quot;: 1,
              &quot;email_id&quot;: 1,
              &quot;file_name&quot;: &quot;foo.jpg&quot;,
              &quot;file_path&quot;: &quot;/home/user/laravel/storage/app/foo.jpg&quot;,
              &quot;url&quot;: &quot;http://127.0.0.1:8000/storage/foo.jpg&quot;
          }
      ]
  }
  </code></pre>
  <p>And this <code>return Email::with('attachments:file_name')-&gt;get();</code> I get <code>attachments</code> as an empty array:</p>
  <pre><code>{
      &quot;id&quot;: 1,
      &quot;email&quot;: &quot;me@me.com&quot;,
      &quot;subject&quot;: &quot;My Subject&quot;,
      &quot;body&quot;: &quot;&lt;html&gt;&lt;body&gt;&lt;h1&gt;My test message.&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;&quot;,
      &quot;created_at&quot;: &quot;2021-02-26T23:32:08.000000Z&quot;,
      &quot;updated_at&quot;: &quot;2021-02-26T23:32:08.000000Z&quot;,
      &quot;attachments&quot;: []
  }
  </code></pre>
  <p>How can I get only this?</p>
  <pre><code>{
      &quot;email&quot;: &quot;me@me.com&quot;,
      &quot;subject&quot;: &quot;My Subject&quot;,
      &quot;body&quot;: &quot;&lt;html&gt;&lt;body&gt;&lt;h1&gt;My test message.&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;&quot;,
      &quot;attachments&quot;: [
          {
              &quot;url&quot;: &quot;http://127.0.0.1:8000/storage/foo.jpg&quot;
          }
      ]
  }
  </code></pre>
  <p>I already did this tries:</p>
  <ol>
  <li><p><code>return Email::with('attachments:url')-&gt;get(['email', 'subject', 'body']);</code></p>
  <p>SQLSTATE[42S22]: Column not found: 1054 Unknown column 'url' in 'field list' (SQL: select `url` from `email_attachments` where `email_attachments`.`email_id` in (0))&quot;</p>
  </li>
  <li><p><code>return Email::with('attachments')-&gt;get(['email', 'subject', 'body', 'url']);</code></p>
  <p>&quot;SQLSTATE[42S22]: Column not found: 1054 Unknown column 'url' in 'field list' (SQL: select `email`, `subject`, `body`, `url` from `emails`)&quot;</p>
  </li>
  <li><p><code>return Email::with('attachments:url')-&gt;get(['email', 'subject', 'body', 'url']);</code></p>
  <p>&quot;SQLSTATE[42S22]: Column not found: 1054 Unknown column 'url' in 'field list' (SQL: select `email`, `subject`, `body`, `url` from `emails`)&quot;</p>
  </li>
  </ol>
  <p>PS.: I'm using <strong>Laravel 8.29.0</strong>.</p>