title: Laravel Eloquent eager loading: children are missing parent reference (sort of N+1)
body: <p>I am currently running on Laravel 5.1.19 and am observing the following issue:</p>
  
  <p>Assume the following models (Students and Teachers as example):</p>
  
  <pre><code>class Teacher extends \Illuminate\Database\Eloquent\Model {
      public function rel_students() {
          return $this-&gt;hasMany(Student::class);
      }
  }
  
  class Student extends \Illuminate\Database\Eloquent\Model {
      public function rel_teacher() {
          return $this-&gt;belongsTo(Teacher::class);
      }
  }
  </code></pre>
  
  <p>If you then query an instance of <code>Teacher</code> and (lazy) eager load its <code>Student</code>s, the <code>-&gt;rel_teacher</code> magic member of all students triggers a fresh query against that teacher:</p>
  
  <pre><code>$teacher = Teacher::with('rel_students')-&gt;find(1);
  // or this, the effect is the same
  $teacher = Teacher::find(1); $teacher-&gt;load('rel_students');
  
  foreach ($teacher-&gt;rel_students as $student)
      echo $student-&gt;teacher-&gt;name . "&lt;br&gt;";
  </code></pre>
  
  <p>Query log for the above code:</p>
  
  <pre><code>SELECT * FROM teachers WHERE id = 1
  SELECT * FROM students WHERE teacher_id IN (1)
  SELECT * FROM teachers WHERE id = 1
  SELECT * FROM teachers WHERE id = 1 # what is going on here?!
  SELECT * FROM teachers WHERE id = 1
  ... and so forth for every student ...
  </code></pre>
  
  <p>The issue: Eloquent has an instance of <code>Teacher</code> #1 when <code>find(1)</code> finishes. I expect eloquent to pass a reference to this very PHP-Objet to the eager loaded <code>Student</code>s so that <code>$student-&gt;teacher</code> returns that reference instead of triggering another query.</p>
  
  <p>another, negative sideffect: even if the queries were no performance issue (which they are!) i'd have hundrets of instances for <code>Teacher</code> #1 fyling around and that is very contrary to the unit-of-work pattern.</p>
  
  <p><strong>Question</strong>: What can i do to make eloquent behave as i expect it to? if not possible: which of the PHP-ORMs are "intelligent" enough and do this simple trick?</p>
  
  <p><strong>UPDATE 1:</strong> I did try to remove the underscore from the relation name - same result.</p>
  
  <p><strong>UPDATE 2:</strong> As mentioned in an answer, <code>with</code>/<code>load</code> with both relations does a almost-perfect job: <code>$teacher-&gt;load('rel_students.rel_teacher')</code>.
  This reduces the queries for the teacher to 2, but that still leaves the issue that <code>$student-&gt;rel_teacher !== $teacher</code>. This is fine until someone modifies either object and then it starts getting hairy.</p>