title: Eloquent: Paginate Two Query Result With Different Condition (Has different with())
body: <p>Hello fellow Laravel programmer.</p>
  <hr />
  <p>I'm on Laravel 5.7 and I'm having 4 tables:</p>
  <p><code>outlets</code> - belongsTo <code>merchants</code>, also belongsToMany <code>promos</code></p>
  <p><code>promos</code> - belongsTo <code>merchants</code>, also belongsToMany <code>outlets</code></p>
  <p><code>merchants</code> - list of merchants that has many promos (hasMany <code>promos</code>, hasMany <code>outlets</code>)</p>
  <p><code>outlet_promo</code> - that connects <code>outlets</code> and <code>promos</code>, only store their IDs</p>
  <hr />
  <h2>Condition 1#:</h2>
  <p>Every merchant's promo that only active in specific outlet has <code>outlet_promo</code> values, such as</p>
  <pre><code>outlet_promo
  outlet_id      promo_id
      1             1
  </code></pre>
  <p>Means that the <code>promo_id</code> 1 only active on <code>outlet_id</code> 1.</p>
  <h2>Condition 2#:</h2>
  <p>Now if a promo active on all merchant's outlets means that no relation on <code>outlet_promo</code>. (I'm avoiding too many storing in <code>outlet_promo</code> table)</p>
  <h2>Fetching Data</h2>
  <p>I'm trying to get all <code>outlets</code> where has active <code>promos</code> that can be <code>active in some outlets only</code> and <code>active in all outlets</code>, resulting me in this two query:</p>
  <ol>
  <li>Getting all <code>outlets</code> where has active promos and its promo available in <strong>every outlets</strong></li>
  </ol>
  <pre class="lang-php prettyprint-override"><code>$outlets_all = Outlet::with(['merchant.promos' =&gt; function ($q) {
              $q-&gt;doesntHave('outlets');
          }])-&gt;whereHas('merchant.promos', function ($q) {
              $q-&gt;whereDate('start_date', '&lt;=',  date('Y-m-d'));
          })-&gt;doesntHave('promos.outlets');
  </code></pre>
  <ol start="2">
  <li>Getting all <code>outlets</code> where has active promos and its promo only active in <strong>selected outlets</strong></li>
  </ol>
  <pre class="lang-php prettyprint-override"><code>$outlets_selected = Outlet::with(['merchant.promos' =&gt; function ($q) {
              $q-&gt;has('outlets');
          }])-&gt;whereHas('promos', function ($q) {
              $q-&gt;whereDate('start_date', '&lt;=',  date('Y-m-d'));
          });
  </code></pre>
  <p><strong>The key difference is in <code>with</code> condition</strong></p>
  <h2>Goals</h2>
  <p>Within these two queries I managed to get the desired output. But now the goal is I want to <strong>paginate</strong> both result in one object so I can lazy load the <code>outlets</code> on the view using AJAX. I want to display list of <code>outlets</code> that has <code>promos</code> (all outlets or selected outlets) with list of active <code>promos</code> that outlet has.</p>
  <h2>What I have tried so far</h2>
  <p>I tried to <code>union()</code> and <code>unionAll()</code> both queries, resulting correct outlets, but has wrong <code>promos</code> list from <code>merchant.promos</code>. It seems that the union operation messes up the promos. Every merchant's promos seems have same value, when actually they should be different.</p>
  <p>I even tried to merge the query with two <code>with()</code> operation like this:</p>
  
  <pre class="lang-php prettyprint-override"><code>$merge = Outlet::with(['merchant.promos' =&gt; function ($q) {
              $q-&gt;has('outlets');
          }], ['merchant.promos' =&gt; function ($q) {
              $q-&gt;doesntHave('outlets');
          }])-&gt;whereHas('promos', function ($q) {
              $q-&gt;whereDate('start_date', '&lt;=',  date('Y-m-d'));
          })-&gt;orWhereHas('merchant.promos', function ($q) {
              $q-&gt;whereDate('start_date', '&lt;=',  date('Y-m-d'));
          })-&gt;doesntHave('promos.outlets');
  </code></pre>
  <p>And it just results the same as the <code>union</code> I did before.</p>
  <p>I'll be tremendously grateful for any help. Thank you.</p>