title: Modify Laravel Eloquent Relations after Models have been retrieved
body: <p>A collection of models and their necessary relations (eager loaded) have been retrieved from the DB. When further processing the results, it becomes necessary to inverse some of the relationships. In order to save resources, no additional relations should be loaded from the DB.</p>
  
  <p>The initial query looks the following:</p>
  
  <pre><code>$resource = Model_A::with([
    'Model_B',
    'Model_C'
  ])-&gt;paginate(25);
  </code></pre>
  
  <p>The initial results look the following:</p>
  
  <pre><code>Collection {
    #items: array:3 [
      0 =&gt; Model_A {
        ...
        #relations: array:2 [
          "Relation_Model_B" =&gt; Model_B { ... },
          "Relation_Model_C" =&gt; Model_C { ... }
        ...
      },
      1 =&gt; Model_A {
        ...
        #relations: array:2 [
          "Relation_Model_B" =&gt; Model_B { ... },
          "Relation_Model_C" =&gt; Model_C { ... }
        ...
      },
      2 =&gt; Model_A {
        ...
        #relations: array:2 [
          "Relation_Model_B" =&gt; Model_B { ... },
          "Relation_Model_C" =&gt; Model_C { ... }
        ...
      }
    ]
  };
  </code></pre>
  
  <p>After transformation the result should look the following:</p>
  
  <pre><code>Collection {
    #items: array:2 [
      0 =&gt; Model_B {
        ...
        #relations: array:1 [
          "Relation_Model_A" =&gt; array:3 [
             0 =&gt; Model_A { ... },
             1 =&gt; Model_A { ... },
             1 =&gt; Model_A { ... }
           ]
        ]
        ...
      },
      1 =&gt; Model_C {
        ...
        #relations: array:1 [
          "Relation_Model_A" =&gt; array:3 [
             0 =&gt; Model_A { ... },
             1 =&gt; Model_A { ... },
             1 =&gt; Model_A { ... }
           ]
        ]
        ...
      }
    ]
  };
  </code></pre>
  
  <p>I already looked into the <code>Illuminate\Database\Eloquent\Builder</code> and <code>Illuminate\Database\Eloquent\Model</code> Classes, but my PHP and Laravel knowledge is not strong enough yet to figure it out on my own.</p>
  
  <p>Thank you for your support! This is much appreciated!</p>