title: How do I only return the parent if child has results in Laravel, if the relations are triple nested?
body: <p>I have the following relations <code>Client</code> &gt; belongsToMany <code>Brand</code> &gt; hasMany <code>Handles</code> &gt; hasMany <code>Content</code>.</p>
  <p>How would I go about only returning <code>Handles</code> if <code>Content</code> has rows, only <code>Brands</code> if <code>Handles</code> has rows and only <code>Client</code> if it also has rows, I am fetching from a PostgreSQL database?</p>
  <p>Heres a scraped version of the models</p>
  <p><strong>Client model</strong></p>
  <pre><code>&lt;?php
  
  namespace App\Models;
  
  use Illuminate\Database\Eloquent\Factories\HasFactory;
  use Illuminate\Database\Eloquent\Model;
  use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  use Illuminate\Database\Eloquent\SoftDeletes;
  
  class Client extends Model
  {
      use HasFactory;
      use SoftDeletes;
  
      protected $hidden = ['pivot'];
  
      protected $fillable = ['title', 'subscription_expire', 'subscription_level'];
  
  
      /**
       * Get brands for client
       *
       * @return BelongsToMany
       */
      public function brands(): BelongsToMany
      {
          return $this-&gt;belongsToMany(
              Brand::class,
              'organized_objects',
              'client_id',
              'object_id'
          )-&gt;wherePivot('object_type', 'brand');
      }
  }
  </code></pre>
  <p><strong>Brand Model</strong></p>
  <pre><code>&lt;?php
  
  namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  use Illuminate\Database\Eloquent\SoftDeletes;
  use Illuminate\Database\Eloquent\Factories\HasFactory;
  use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  
  class Brand extends Model
  {
      use HasFactory;
      use SoftDeletes;
  
      /**
       * Get handles for brand
       *
       * @return BelongsToMany
       */
      public function handles(): BelongsToMany
      {
          return $this-&gt;belongsToMany(Handle::class, 'handle_pivots', 'handle_pivot_id', 'handle_id');
      }
  }
  </code></pre>
  <p><strong>Handle Model</strong></p>
  <pre><code>&lt;?php
  
  namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  use Illuminate\Database\Eloquent\Relations\HasMany;
  use Illuminate\Database\Eloquent\Factories\HasFactory;
  
  class Handle extends Model
  {
      use HasFactory;
  
      protected $fillable = ['name', 'platform'];
  
      /**
       * Get content of handle.
       *
       * @return HasMany
       */
      public function content(): HasMany
      {
          return $this-&gt;hasMany(PureContent::class, 'handle_id');
      }
  }
  
  </code></pre>
  <p>Currently I am doing the following in my controller to get the content I need for the handles.</p>
  <pre><code>/**
   * Client metrics
   *
   * Displays metrics for the given client
   *
   * @param Request $request
   * @param Client  $client
   *
   * @return JsonResponse
   */
  public function index(Request $request, Client $client): JsonResponse
  {
      $to = $request-&gt;get('to');
      $from = $request-&gt;get('from');
      $to_date = $to ? Carbon::parse($to) : Carbon::now();
      $from_date = $from ? Carbon::parse($from) : $to_date-&gt;copy()-&gt;startOfYear();
  
      $client_content = $client-&gt;with([
          'brands' =&gt; function ($q) use ($to_date, $from_date) {
              $q-&gt;with([
                  'handles' =&gt; function ($q) use ($to_date, $from_date) {
                      $q-&gt;whereHas('content', function ($q) use ($to_date, $from_date) {
                          $q-&gt;whereBetween('created_at', [$from_date, $to_date]);
                      })-&gt;with(['content' =&gt; function ($q) use ($to_date, $from_date) {
                          $q-&gt;whereBetween('created_at', [$from_date, $to_date]);
                      }])-&gt;get();
                  },
              ])-&gt;get();
          },
      ])-&gt;get();
  
      return response()-&gt;json($client_content);
  }
  </code></pre>
  <p>But when I am returning the collection, I still get the related collection just empty.</p>
  <p>And how much would it take to add for example a Collection model on the query? It has the same setup as brands &gt; handles &gt; content?</p>