title: Eloquent relations from multiple joined tables
body: <p>Let's say we have a table <code>users</code> and we are left joining multiple tables to it.</p>
  <pre class="lang-php prettyprint-override"><code>        $users = User::query()
              -&gt;select('users.id', 'bananas.id as banana_id', 'dogs.id as dog_id')
              -&gt;leftJoin('bananas', 'banana.user_id', '=', 'users.id')
              -&gt;unionAll($usersWithDogs) // a similar query with a left join on `dogs`
              -&gt;orderByDesc('users.created_at')
              -&gt;paginate(...);
  </code></pre>
  <p>We end up with a collection of <code>User</code> models with attributes <code>id</code>, <code>dog_id</code>, <code>banana_id</code>.</p>
  <p>Now imagine we want to eager load these, but the eloquent relations are based on the one to many relationships, <code>$user-&gt;dogs</code>, <code>$user-&gt;bananas</code>.</p>
  <p>Trying to find a solution that will do all the following:</p>
  <p>a) not break pagination<br />
  b) allow ordering on the user table<br />
  c) allow eager loading<br />
  d) use clean code<br />
  e) end up with a collection of users</p>
  <p>Brainstorming so far has led to the following options:</p>
  <ol>
  <li>A union of <code>bananas</code> and <code>dogs</code>, eager load the <code>user</code> relation, then invert the collection (messy code)</li>
  <li>Dynamic relationships created on <code>User</code>, possibly with a macro on <code>\Illuminate\Database\Eloquent\Builder</code>. Maybe by leveraging <code>Model::resolveRelationUsing()</code></li>
  <li>Manual eager loading with a union select with a left join to each table in each arm of the union, then a <code>whereIn()</code> to get the related records</li>
  <li>Restructure the relations so that there is a polymorphic many to many relationship between users and other entities
  e.g.</li>
  </ol>
  <pre><code>    user_id | model_type        | model_id
      1       | App\Models\Banana | 2
      1       | App\Models\Dog    | 5
  </code></pre>
  <p>Maybe I'm missing something obvious...?</p>